I can understand this:
iex(7)> outside_val = 5
5
iex(8)> print = fn() -> IO.puts(outside_val) end
#Function<20.90072148/0 in :erl_eval.expr/5>
iex(9)> print.()
5
:ok
What I don't get so much is why is it Elixir allows the print function to be defined even if outside_val is not defined and only error out later? There is no way to pass in 'outside_val' after the closure has been defined anyway, so isn't it better for Elixir to check for existence of variable during creation ?
What I mean is this:
iex(2)> print = fn () -> IO.puts(outside_val) end
#Function<20.90072148/0 in :erl_eval.expr/5>
iex(3)> outside_val = 5
5
iex(4)> print.()
** (RuntimeError) undefined function: outside_val/0