r/purescript • u/daigoro_sensei • Nov 10 '22
Why does this compile? It just warns me that my var has been shadowed...shouldn't the compiler disallow this mutation?
6
Upvotes
r/purescript • u/daigoro_sensei • Nov 10 '22
6
u/friedbrice Nov 10 '22
it's not mutation.
will this program print 2 or 5? it'll print 2, because there is no mutation going on.
2
u/peterjoel Nov 10 '22
It compiles because it's a language feature. It warns you in this case, because it's probably not what you intended to do.
A common reason to do this intentionally is when you change the type of a variable. It would be annoying to have to come up with a different name, and shadowing an existing name can make the code nicer.
For example: