开发者

Beginner question on Ocaml Refs

开发者 https://www.devze.com 2023-03-06 04:51 出处:网络
let x = ref 100 in let f () = !x in let x = ref 50 in ??? ; f () You are supposed to get the answer to be 50 by plugging something into the ??? (not shadowing). But I don\'t know how to change the v
let x = ref 100 in
let f () = !x in
let x = ref 50 in
??? ; f ()

You are supposed to get the answer to be 50 by plugging something into the ??? (not shadowing). But I don't know how to change the value of your orig开发者_如何学Pythoninal ref now, considering x := 50 is going to apply to the SECOND x now....


If it really is a "beginner question", that's probably not the expected answer. But I don't know what the "expected answer" is, so let's hack around instead.

Obj.(obj (field (repr f) 1)) := 42;;

relevant link


There is no way to access original x now. So.. shadow f!

0

精彩评论

暂无评论...
验证码 换一张
取 消