F# - Function with no arguments?

后端 未结 2 1440
暗喜
暗喜 2020-12-17 07:39

When thinking in a functional mindset, given that functions are supposed to be pure, one can conclude any function with no arguments is basically just a value.
However,

2条回答
  •  渐次进展
    2020-12-17 08:05

    I think what you want is lazy.

    let resource = 
        lazy(
            // expensive value init here
        )
    

    Then later when you need to read the value...

    resource.Value
    

    If you never call the Value property, the code inside the lazy block never gets run, but if you do call it, that code will be run no more than once.

提交回复
热议问题