Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is a variable not immutable?

Tags:

elm

Why is varx not immutable in this code?

y = { g = 'a' }
z = { g = 1 }

varx = foo y

varx = foo z

Anybody knows? Thanks.

like image 980
djangodeveloper Avatar asked Oct 17 '25 16:10

djangodeveloper


1 Answers

I guess that this code is executed in the Elm REPL. Immutable variables behave a little differently there. From the Beginning Elm book:

https://elmprogramming.com/immutability.html

The repl works a little differently. Whenever we reassign a different value to an existing constant, the repl essentially rebinds the constant to a new value. The rebinding process kills the constant and brings it back to life as if the constant was never pointing to any other value before.

When compiling Elm code the ordinary way with Elm make, this code would lead to an error.

like image 158
Holger L Avatar answered Oct 19 '25 13:10

Holger L