New EEP draft: Pinning operator ^ in patterns

Richard Carlsson carlsson.richard@REDACTED
Wed Jan 20 20:59:49 CET 2021


Den ons 20 jan. 2021 kl 17:27 skrev Raimo Niskanen <
raimo+erlang-questions@REDACTED>:

> 1: foo(Y) ->
> 2:      F = fun (Y) ->
> 3:           FF = fun (Y) ->
> 4:                    ^Y = Y - 1
> 5:                end,
> 6:           FF(Y + 1)
> 7:          end,
> 8:      F(Y + 1).
>
> I guess that would produce warnings for shadowing on line 2 and 3.
>

Yes, just as usual. Nothing changes there.


> I guess that ^Y on line 4 refers to the Y bound on line 2.
>

No, it always refers to the nearest surrounding binding, i.e., the one on
line 3. If you look at the fun on line 3 in isolation: fun (Y) -> ^Y = Y -
1 end, it should be clear that ^Y in the = means the Y bound in the fun
head.

The fundamental principle I'm trying to stick to is that the general
semantics of an expression should be the same regardless of the context you
might cut and paste it into. The problem with the current interpretation of
"X = ..." or "case ... of X -> ..." is that it might be an equality test,
or it might be a binding that always succeeds, depending on what you do
with the surrounding code.

By saying explicitly "^X = ..." or "case ... of ^X -> ..." it becomes
always a test, never a binding, no matter what else you edit. If you also
ensure your code is always clean from warnings about unmarked already-bound
variables, the other direction is also true: variables intended to be new
in a pattern will never turn into tests by accident.


> I guess that warn_unpinned_vars would not produce any warnings.
>

In this example, no, since variables in fun heads are shadowing.


> Can I match against the Y bound on line 1 from within FF/1?
>

No, that's buried by two levels of shadowing. In the head of F on line 2
you could refer to the Y on line 1, but that's it. Even in the body of F,
the outer Y would not be available.

        /Richard
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20210120/8913164a/attachment.htm>


More information about the erlang-questions mailing list