Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

immutability?

    Interactive Elixir (1.19.0) - press Ctrl+C to exit (type h() ENTER for help)
    iex(1)> x = 1
    1
    iex(2)> x = 2
    2
    iex(3)>

What's immutable about elixir? It's one of the things which I MISS from Erlang -- immutability.


This is like saying Haskell doesn't have immutability because it has the state monad, or that rust doesn't because you can shadow with let.

Data is immutable and thats much more important than whether local variables can be modified imo.


Shadowing is not mutability. The former isn't affected by control flow nor does it affect closures.


What you wrote is roughly equivalent to this in C:

  {
    const int x = 1;
    {
      const int x = 2;
    }
  }
which is to say, there are two different `x` in there, both immutable, one shadowing the other. You can observe this if you capture the one that is shadowed in a closure:

  iex(1)> x = 1
  1
  iex(2)> f = fn () -> x end
  #Function<43.113135111/0 in :erl_eval.expr/6>
  iex(3)> x = 2
  2
  iex(4)> f.()
  1




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: