在函数内取消引用 ref 会产生不同的结果。为什么?
在此示例中,正在急切地评估 get_final_answer,并且始终返回 0.0。我认为包含 refs 的表达式由于其固有的可变特性而受到不同的对待(在这种情况下不会急于评估)。我预计它会返回7.0。
let FinalAnswer = ref 0.0
let get_final_answer = !FinalAnswer
let rec eval_expr_fail =
FinalAnswer := 7.0
get_final_answer // fails, returns 0.0
let rec eval_expr_works =
FinalAnswer := 7.0
!FinalAnswer // works, return 7.0
如何在更新的块之外取消引用 FinalAnswer?
In this sample, get_final_answer is being eagerly evaluated, and always returns 0.0. I thought expressions containing refs were treated differently (and not eagerly evaluated in this case) due to their inherently mutable characteristics. I expected it to return 7.0.
let FinalAnswer = ref 0.0
let get_final_answer = !FinalAnswer
let rec eval_expr_fail =
FinalAnswer := 7.0
get_final_answer // fails, returns 0.0
let rec eval_expr_works =
FinalAnswer := 7.0
!FinalAnswer // works, return 7.0
How do I dereference FinalAnswer outside the block where I updated it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
let get_final_answer = !FinalAnswer
中的get_final_answer
是浮点值,而不是函数。它的值为0.0,一旦赋值就与FinalAnswer
无关。将其作为一个函数得到你想要的:
get_final_answer
inlet get_final_answer = !FinalAnswer
is a float value, not a function. It is the value of 0.0, and has nothing to do withFinalAnswer
once the value is assigned.Making it as a function gets what you want: