2016-08-23 190 views
2

這是一個非常簡單的類的靜態屬性時,每一次變化(fsi.exe)F#靜態成員訪問

it changes...

什麼是我的目的是存儲值靜態成員:

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

它正在做它應該做的事情。每次你打電話時你都會得到一個隨機數字。你能否詳細說明你想要做什麼以及你的期望是什麼。 'Dispose'意味着擺脫實現IDisposable的東西。 – s952163

+0

我知道我可以使用下面的代碼,但不知道有什麼區別: type test()= static member val ttc =(new Random())。Next()with get,set – ingted

+0

這是一個auto與後臺字段屬性。如果你需要靜態成員,將會做你想做的。 – s952163

回答

4

這裏的區別很短的例子:

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