There are several steps when defining a function in Erlang (and in Elixir, since it is built on top of ErlangVM).
First, you tokenize the input:
{ok, Ts, _} = erl_scan:string("fun() -> Z + 1 end.").
Then you create an abstract syntax tree:
{ok, [ListAST]} = erl_parse:parse_exprs(Ts).
The final step is to evaluate it with:
Bindings = [{'Z', 1}]. erl_eval:expr(ListAST, Bindings).
In the last step, Erlang can see that there are undefined variables and throw an exception.
In Elixir, most of the language functions are implemented as macros, so the last step is not performed during function definition, but when it is called. I'm not sure if you can check if all the variables are connected inside the macro definition. If possible, it will be a cool solution.
source share