[erlang-questions] Matching binaries and shadowed variables

virtan <>
Wed Nov 27 19:56:20 CET 2013


Does anybody know why there is a such thing as shadowed variables in Erlang?
What was the reason to prevent typical "matching" approach for closures?

It is even more unclear in matching binaries:

1> A = <<"hello">>, ASize = size(A),
      lists:map(
          fun (<<A1:ASize/binary, _/binary>>) when A1 == A -> match;
                (_) -> nomatch end,
          [<<"hello world">>, <<"nohello">>]).
[match,nomatch]
2>

You can't use A instead of A1 (it creates new variable, shadowing outer A),
but should use ASize inside the pattern (otherwise you'll get unbound error during compilation).

-- 
virtan /  / http://www.virtan.com




More information about the erlang-questions mailing list