Doing some erlang review as it’s been a while since I’ve used it. Something I’ve wondered about. Can someone point me to some code or an explanation of how elixir allows rebinding variables? Also interesting to me and potentially relevant is pin operator implementation.
2 Likes
It just compiles to different variables behind the scenes.
a = 1
a = 2
essentially becomes
a1 = 1
a2 = 2
8 Likes
That makes sense, is it really that simple though? Everything becomes an ordinal version of itself postfixed with a number?
4 Likes
Yes:
iex(1)> defmodule Example do
...(1)> def test(a) do
...(1)> a = a * 2
...(1)> a = a + 1
...(1)> a
...(1)> end
...(1)> end
iex(2)> beam = elem(v(), 2)
iex(3)> {:ok, {_, [{'Dbgi', dbgi}]}} = :beam_lib.chunks(beam, ['Dbgi'])
iex(4)> {:debug_info_v1, backend, data} = :erlang.binary_to_term(dbgi)
iex(5)> {:ok, ast} = backend.debug_info(:erlang_v1, Example, data, [])
iex(6)> IO.puts(:erl_prettypr.format(:erl_syntax.form_list(ast)))
#
# ...snip...
#
test(_a@1) ->
_a@2 = _a@1 * 2,
_a@3 = _a@2 + 1,
_a@3.
17 Likes