GS *_*ica 15
Brian的答案的替代版本,b最多只评估一次,但如果B从未使用过,则根本不评估它
type Test(a:float) =
// constructor
let b = lazy
printfn "oh no"
a * 2.
// properties
member this.A = a
member this.B = b.Value
Run Code Online (Sandbox Code Playgroud)
Bri*_*ian 13
这是一个属性; 你基本上是在打电话给get_b()会员.
如果希望效果在构造函数中发生一次,则可以使用类:
type Test(a:float) =
// constructor
let b = // compute it once, store it in a field in the class
printfn "oh no"
a * 2.
// properties
member this.A = a
member this.B = b
Run Code Online (Sandbox Code Playgroud)