Earlier quoted context omitted.
iex(1)> a=10 10 iex(2)> a=11 11 Because underneath, it’s doing A0 = 10. A1 = 11. You might not like it, because it feels like mutation, but it’s not, it’s rebinding. Just consider this as a syntactic sugar. It’s useful when doing conn = conn |> apply_some_change() In the end, it does generate valid bytecode for the BEAM, and immutability is respected. BTW You might prefer Erlang syntax, but You would lose |> José Val…
Genuine question: From an application developer's perspective, what's the difference between mutating a value and transparently rebinding an old name to a new value? Is it just that in the latter case other references don't pick up the changes? So with rebinding we don't have something like a = 10 b = a a = 11 print(b) // 11 ?
def func() do
a = 10
def func2() do
a + 1
end
a = 20
func2()
end
Mutation would have func() return 21. Rebinding has it return 11.Likewise, mutating languages typically allow for a method to modify its arguments when those arguments are objects
public void modify(MyObject a){
a.changed = true;
}
public bool test_modify(){
MyObject b = new MyObject();
b.changed = false;
modify(b);
return b.changed
}
test_modify will return true in languages that allow mutation.