Reputation: 45
Here is a very simple class with static property:
[<AbstractClass; Sealed>]
type test () =
static member ttc = (new Random()).Next()
When I access ttc, it always changes... like this: (in fsi.exe)
What is my purpose is to store values in static member:
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
But each time I access typDimTablesArray.DimDatetime.Length It just query the database again and never store the data in the static member...
Upvotes: 2
Views: 160
Reputation: 6324
Here's a short example of the differences:
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
Also, you could just create an instance of your type and pass in whatever object you need to work on. Now if it's something lazy you might need to cache it:
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
Upvotes: 3