F#记录成员评估

编程入门 行业动态 更新时间:2024-10-19 10:26:17
本文介绍了F#记录成员评估的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

为什么每次通话都会评估t.b?并且有什么方法可以使它只评估一次?

Why is t.b evaluated on every call? And is there any way how to make it evaluate only once?

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

推荐答案

这是一个属性;您基本上是在呼叫get_b()成员.

It's a property; you're basically calling the get_b() member.

如果您希望效果在构造函数中发生一次,则可以使用一个类:

If you want the effect to happen once with the constructor, you could use a class:

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

更多推荐

F#记录成员评估

本文发布于:2023-11-10 02:38:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1574106.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:成员

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!