Great article Alexei!
Just a few things I want to comment on:
Although variables defined at the top level are accessible inside modules, the modules cannot modify them (just like closures). I couldn’t find an explanation of this.
defmodule M do
x = 1
# shadowing in action: the 'x' in the argument list creates a variable
# local to the function clause's body and has nothing to do with the
# previously defined 'x'
def baz(x), do: x
end
I wouldn’t call the above shadowing, since there is no way to refer to the module level x
from inside the function. This would also be a good place that to explain that things inside unquote
uses the module level context.
I agree that cond
should no leak in clause heads, this is inconsistent with case
today. Although case
and receive
‘s ->
are matchers, while cond
is just a normal expression, so they are not fully comparable.
I don’t really see the rationale for if
to not leak in the head. The reason comprehensions don’t leak is because they are implemented with anonymous functions (we have discussed making the new comprehensions leak so that the variables would work as accumulators in a reduce). Having a special scoping rule for if
‘s head only complicates things, why does the head not leak but the body does? I am also pretty sure we cannot implement this without making if
a special form (this might be the reason why cond
has the current behaviour).
--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Although variables defined at the top level are accessible inside modules, the modules cannot modify them (just like closures). I couldn’t find an explanation of this.
I wouldn’t call the above shadowing, since there is no way to refer to the module levelx
from inside the function. This would also be a good place that to explain that things insideunquote
uses the module level context.
I don’t really see the rationale for if
to not leak in the head.
cond do<condition> -> <then_body>true -> <else_body>endif <condition> do <then_body> else <else_body>
# new scopedefmodule Foo do# new scopedef foo do# new scopeif true do# new scopeelse# new scopeendendend
--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
I've just updated the example with `cond` (in the section https://github.com/alco/elixir/wiki/Scoping-Rules-in-Elixir-%28and-Erlang%29#case-like-clauses). It assumes the bug that causes leaking of variables bound in clause conditions is fixed.
--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.