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?
get_final_answerinlet get_final_answer = !FinalAnsweris a float value, not a function. It is the value of 0.0, and has nothing to do withFinalAnsweronce the value is assigned.Making it as a function gets what you want: