Skip to main content

Scope

Erlang is very relaxed in its scoping rules, and does not, in many cases, abide by lexical scoping.

Joining scopes#

In the case where control flow splits into several branches, if a variable is bound in all branches, the variable will be available in the outer scope as well.

joining_scope(A) ->
case A of
1 ->
B = a,
C = b;
2 ->
B = c
end,
% C is NOT in scope, it was only bound in one arm of the case clause.
% foo(C),
% B IS in scope, it was bound in both case clause arms.
foo(B).

This works on both case expressions, if expressions and receive expressions.