The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

39590 (3) [Avatar] Offline
#1
On page 7 the author writes:

A constant assigns a name to a value. Once assigned, this name cannot be later reassigned to a different value in the same scope.


But in my elm-repl I can:

> fakeConst = 1
1 : number
> fakeConst = 2
2 : number
> fakeConst
2 : number


Why?
9821 (4) [Avatar] Offline
#2
It'd be a pain in the ass if you had to restart the REPL all the time and reenter all your previous expression. The point of the REPL is quick exploration.
tempusfugit (144) [Avatar] Offline
#3
Note that this only applies to top level names
> fn y = \
|     let \
|         x = 1 \
|         x = 2 \
|     in \
|         x + y
-- DUPLICATE DEFINITION -------------------------------------- repl-temp-000.elm

There are multiple values named `x` in this let-expression.

7|         x = 2 
           ^
Search through this let-expression, find all the values named `x`, and give each
of them a unique name.
However allowing redefinition/rebinding of top level names apparently has some unintended consequences:
elm-lang/elm-repl/issue #101: Top-level variables are mutable in closures
A closure doesn't have to capture what cannot change, so weird stuff starts happening when change is in fact possible.

Also note that shadowing is permitted in code and the REPL.
> x = 2
2 : number
> fn x = \
|     let \
|         x = 3 -- shadowing function parameter \
|     in \
|         x
<function:_user$project$Repl$fn> : a -> number
> fn 5
3 : number
> x
2 : number
Finally:
> fakeConst = 1 \
| fakeConst = 2
-- DUPLICATE DEFINITION -------------------------------------- repl-temp-000.elm

Naming multiple top-level values `fakeConst` makes things ambiguous. When you
say `fakeConst` which one do you want?

5| fakeConst = 2
   ^^^^^^^^^
Find all the top-level values named `fakeConst` and do some renaming. Make sure
the names are distinct!
So even top level values can only be changed between evaluations. During any single evaluation names and values remain immutable.