elm-repl
elm-repl copied to clipboard
Top-level variables are mutable in closures
Consider the following session:
> x = 3
3 : number
> xThunk = \_ -> x
<function> : a -> number
> x = "Hello"
"Hello" : String
> xThunk ()
"Hello" : String
The variable x
doesn't get closed over. Instead, the function just refers to the name "x", and when we shadow x with a new value, it mutates the closed over value, and affects the function we defined earlier.
Is this by design, a kind of "hot-swapping"? Or is this just a bug in how it's implemented that allows mutation to leak through?
@JoeyEremondi I see it as reactive part, it's not variables, it's definitions:
> a = 1
1 : number
> b = a + 2
3 : number
> a = 5
5 : number
> b
7 : number
So in your example closure gets "function with no arguments" not variable.
If it's reactive, then a variable shouldn't be able to change type, like in my example. On Feb 9, 2016 12:12 AM, "Igor Kapkov" [email protected] wrote:
@JoeyEremondi https://github.com/JoeyEremondi I see it as reactive part, it's not variables, it's definitions:
a = 1 1 : number b = a + 2 3 : number a = 5 5 : number b 7 : number
So in your example closure gets "function with no arguments" not variable.
— Reply to this email directly or view it on GitHub https://github.com/elm-lang/elm-repl/issues/101#issuecomment-181754282.