[erlang-questions] [eeps] Maps

Anthony Ramine n.oxyde@REDACTED
Thu May 9 00:50:07 CEST 2013

```Hello Björn-Egil,

So what happens with the following code?

X = 2, F = fun (X, <<X:X>>) end.

If we allow to bind variables derived from the environment, shouldn't we forbid confusing expressions where a variable is both shadowed and used?

With the current bindings, this is equivalent to:

> X = 2, F = fun (Y, <<Y:X>>) -> {X,Y} end.
#Fun<erl_eval.12.17052888>
> F(1, <<1:2>>).
{2,1}
> F(1, <<1:1>>).
** exception error: no function clause matching
erl_eval:'-inside-an-interpreted-fun-'(1,<<1:1voilà>>)

With the new bindings, this could mean:

> X = 2, F = fun (Y, <<Y:Y>>) -> {X,Y} end.
#Fun<erl_eval.12.17052888>
> F(1, <<1:1>>).
{1,1}
> F(1, <<1:2>>).
** exception error: no function clause matching
erl_eval:'-inside-an-interpreted-fun-'(1,<<1:2>>)

If variables from the outer environment are still used over the ones from the pattern's, the current behaviour would still be exhibited; but it would be weird to have such backwards bindings.

What I suggest on that matter is to forbid variables both used and shadowed at the same time in a pattern when maps are introduced, or maybe even before. I can write a patch for that.

On the matter of the patterns themselves, will this be allowed?

fun (<<A:B,C:D>>, <<B,D:A>>) -> {A,B,C,D} end.

To fully use the limited scope of patterns, we could also broaden the support of constant expressions in patterns to guard expressions using only variables bound in both environments. As the question of dependencies is already answered by the previous convoluted pattern, allowing guard expressions to be used that way wouldn't be difficult.

fun (<<A:B*2,C:D*3>>, <<B,D:A div 2>>) -> {A,B,C,D} end.
keyreplace(element(N, Tuple0), N, [Tuple0|L], Tuple) -> [Tuple|L].

I find this last example quite soothing. Obviously, that can be done already in Erlang:

keyreplace(Key, N, [Tuple0|L], Tuple) when Key =:= element(N, Tuple0) -> [Tuple|L].

But what about that one?

decode(<<5, Len, Data:(Len-1)/binary>>) -> {p5,Data};
% decode other packets.

This one is less obvious to rewrite in current Erlang and I won't bother to do it.

Regards,

--
Anthony Ramine

Le 8 mai 2013 à 16:18, Björn-Egil Dahlberg a écrit :

> function(Skip, <<_:Skip/binary, Value:Size, _/bits>>, Size) -> Value.

```