一段时间以来, F # 支持使用[<ReflectedDefinitionAttribute>]
. 懒惰有什么类似的吗?
例如
member __.Quoted ([<ReflectedDefinitionAttribute>] quotation:Expr<'T>) = ...
member __.Thunked ([<LazyAttribute>] thunk:Lazy<'T>) = ...
我想我可以使用类似的东西
member __.Quoted ([<ReflectedDefinitionAttribute>] quotation:Expr<'T>) =
Lazy (evaluate (<@ fun () -> %quotation @>)) // evaluate using Unquote or similar
但这不会很昂贵吗?
更新:
我发现了一个 hack,它不完全是我想要的,但它给出了正确的行为。
type Signal = Signal with
member __.Return x = x
member __.Delay (f:unit -> _) = f
let a = Signal { return randint }
let b = Signal { return randint }
let c = Signal { return a() + b() }