3 ms·
When combined with lexically scoped closures, if you try to shadow your value bindings in a functional language and expect it to work the same way as mutating a
by kyllo 13y ago
When combined with lexically scoped closures, if you try to shadow your value bindings in a functional language and expect it to work the same way as mutating a variable does in an imperative language, you're in for a surprise.
- marssaxman 13y agoSure, you have to restrict the scope in which rebinding is legal, but I don't see how it's fundamentally any different than the use of a phi-node in SSA form.
- kyllo 13y agoHere's an SML example: - val a = 1; val a = 1 : int - fun f() = a; val f = fn : unit -> int - f(); val it = 1 : int - val a = 2; val a = 2 : int - f(); val it = 1 : int // a is still bound to 1 as far as f() is concerned Now in Javascript: > var a = 1; undefined > function f(){return a}; undefined > f(); 1 > a = 2; 2 > f(); 2 > If you bind a val, then you define a function that references that val, then you later shadow the val binding, then call the function again, the function still sees the earlier val binding, because it's a closure of the environment at the point where the function was defined, not at the point where it was called. This is unlike variable assignment in imperative languages.