2013-04-18 47 views
0

如何將value設置爲Item struct?我嘗試了以下兩種類型,但都以value must be mutable錯誤結束。如何使用F#將結構體的值設置爲'Item`#

module Test1 = 
    [<Struct>] 
    type Test (array: float []) = 
    member o.Item 
     with get i = array.[i] 
     and set i value = array.[i] <- value 

    let test = Test [|0.0|] 
    test.[0] <- 4.0 

module Test2 = 

    [<Struct>] 
    type Test = 
    val mutable array: float [] 
    new (array: float []) = { array = array } 
    member o.Item 
     with get i = o.array.[i] 
     and set i value = o.array.[i] <- value 

    let test = Test [|0.0|] 
    test.[0] <- 4.0 

回答

2

請嘗試更換:

let test = Test [|0.0|] 

有:

let mutable test = Test [|0.0|] 
+0

這是它。現在它可以工作。謝謝。 –

相關問題