每次访问时 F# 静态成员都会更改
F# static member changes each time when accessed
这是一个非常简单的 class 和静态 属性:
[<AbstractClass; Sealed>]
type test () =
static member ttc = (new Random()).Next()
当我访问 ttc 时,它总是发生变化...像这样:(在 fsi.exe)
我的目的是在静态成员中存储值:
type typDimTablesArray () =
static member DimApplication = typDimTables.DimApplication |> Seq.toArray
static member DimApplicationState = typDimTables.DimApplicationState |> Seq.toArray
static member DimDatetime = typDimTables.DimDatetime |> Seq.toArray
static member DimDbQuery = typDimTables.DimDbQuery |> Seq.toArray
static member DimDeveloper = typDimTables.DimDeveloper |> Seq.toArray
static member DimPlatform = typDimTables.DimPlatform |> Seq.toArray
但是我每次访问typDimTablesArray.DimDatetime.Length
它只是再次查询数据库,从不将数据存储在静态成员中...
以下是差异的简短示例:
type Test3() =
let random = new System.Random()
let y = random.Next()
member __.X = random.Next()
member __.Y = y
static member val Z = (new Random()).Next()
let x = Test3()
x.X
x.X
x.Y
x.Y
Test3.Z
Test3.Z
此外,您可以只创建您的类型的实例并传入您需要处理的任何对象。现在,如果它很懒,你可能需要缓存它:
let rnd = new System.Random()
let rnds = Seq.init 10 (fun _ -> rnd.Next())
type Test4(rndsX:int seq) =
let xx = rndsX |> Seq.cache
member __.Length = xx |> Seq.toArray |> Seq.length
member __.First = xx |> Seq.head
member __.Last = xx |> Seq.last
这是一个非常简单的 class 和静态 属性:
[<AbstractClass; Sealed>]
type test () =
static member ttc = (new Random()).Next()
当我访问 ttc 时,它总是发生变化...像这样:(在 fsi.exe)
我的目的是在静态成员中存储值:
type typDimTablesArray () =
static member DimApplication = typDimTables.DimApplication |> Seq.toArray
static member DimApplicationState = typDimTables.DimApplicationState |> Seq.toArray
static member DimDatetime = typDimTables.DimDatetime |> Seq.toArray
static member DimDbQuery = typDimTables.DimDbQuery |> Seq.toArray
static member DimDeveloper = typDimTables.DimDeveloper |> Seq.toArray
static member DimPlatform = typDimTables.DimPlatform |> Seq.toArray
但是我每次访问typDimTablesArray.DimDatetime.Length 它只是再次查询数据库,从不将数据存储在静态成员中...
以下是差异的简短示例:
type Test3() =
let random = new System.Random()
let y = random.Next()
member __.X = random.Next()
member __.Y = y
static member val Z = (new Random()).Next()
let x = Test3()
x.X
x.X
x.Y
x.Y
Test3.Z
Test3.Z
此外,您可以只创建您的类型的实例并传入您需要处理的任何对象。现在,如果它很懒,你可能需要缓存它:
let rnd = new System.Random()
let rnds = Seq.init 10 (fun _ -> rnd.Next())
type Test4(rndsX:int seq) =
let xx = rndsX |> Seq.cache
member __.Length = xx |> Seq.toArray |> Seq.length
member __.First = xx |> Seq.head
member __.Last = xx |> Seq.last