[erlang-questions] Inconsistent shadowing of variables in a fun

Michel Boaventura michel.boaventura@REDACTED
Mon Jan 30 00:00:14 CET 2017


According to Joe's Programming Erlang(page 103): Size must be an expression
that evaluates to an integer. In pattern matching, Size must be an integer
or a bound variable whose value is an integer.

So the N on the pattern match is not a new variable like X.

Em 29 de jan de 2017 20:42, "José Valim" <jose.valim@REDACTED>
escreveu:

>From my understanding, the N in this case can never be an unbound variable.
If you rename N to M, then it won't compile, so it would not be possible
for it to shadow anything.

It is similar to map keys. They are always treated as values, even when
inside patterns.


On Sun, Jan 29, 2017 at 23:07 Robert Virding <rvirding@REDACTED> wrote:

> When defining a fun the shadowing of variables occurring in arguments is
> inconsistent. So with
>
> a(X, N) ->
>     fun (<<X,Y:N,_/binary>>) -> {X,Y} end.
>
> the X in the fun arguments shadows the X from before it while the N is
> imported and used. An example:
>
> 1> c(bt).
> bt.erl:8: Warning: variable 'X' is unused
> bt.erl:9: Warning: variable 'X' shadowed in 'fun'
> {ok,bt}
> 2> f(G),G=bt:a(34,8).
> #Fun<bt.1.1070726>
> 3> G(<<1,2,3,4,5>>).
> {1,2}
> 4> f(G),G=bt:a(34,16).
> #Fun<bt.1.1070726>
> 5> G(<<1,2,3,4,5>>).
> {1,515}
>
> Why the difference? Shouldn't we be consistent in shadowing all variables?
>
> Robert
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
-- 


*José Valim*
www.plataformatec.com.br
Skype: jv.ptec
Founder and Director of R&D

_______________________________________________
erlang-questions mailing list
erlang-questions@REDACTED
http://erlang.org/mailman/listinfo/erlang-questions
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20170129/2ffc26e8/attachment.htm>


More information about the erlang-questions mailing list