这是一个非常简单的静态属性类:
[<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 它只是再次查询数据库,从不将数据存储在静态成员中......
答案 0 :(得分:3)
以下是差异的简短示例:
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