gpt4 book ai didi

F#记录成员评估

转载 作者:行者123 更新时间:2023-12-04 00:05:41 24 4
gpt4 key购买 nike

为什么每次调用都要评估 t.b?有没有办法让它只评估一次?

type test =
{ a: float }
member x.b =
printfn "oh no"
x.a * 2.

let t = { a = 1. }
t.b
t.b

最佳答案

将评估 b 的 Brian 答案的替代版本最多一次,但如果 B 根本不会评估它从不使用

type Test(a:float) =
// constructor
let b = lazy
printfn "oh no"
a * 2.
// properties
member this.A = a
member this.B = b.Value

关于F#记录成员评估,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2873125/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com