New EEP draft: Pinning operator ^ in patterns

Richard Carlsson carlsson.richard@REDACTED
Tue Jan 19 14:38:43 CET 2021


Den tis 19 jan. 2021 kl 14:25 skrev Fernando Benavides <
elbrujohalcon@REDACTED>:

> Oh! I didn't understand that correctly.
> In that case, what would be the meaning of [X, ^X] = something()? Will
> that even be a valid expression?
>
> I'm assuming that f(X, X) ->... Will indeed need to be written as f(X, ^X)
> ->..., right?
>

^X always means a single thing - "the value of X in the surrounding scope".
Any unmarked X has the same meaning as today.

In a simple match expression [X, ^X] = something(), or a case clause "case
something() of [X, ^X] -> ..." you'd get a warning about the unmarked X (if
you enable the warnings), but it keeps it's existing meaning, which is the
same as ^X for these kinds of patterns.

In a fun head or list comprehension generator pattern, you'd get the usual
warning that the unmarked X is shadowing (assuming it has a binding).

If you do mark a variable as ^X, and it *doesn't* have a previous binding,
that's a normal "unbound variable" error, just as if you had written "X1
when X =:= X1 -> ..." and X doesn't actually exist. This means that the
clause doesn't accidentally start to match on any value in case you rename
X to something else above.


        /Richard
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20210119/87e3d6ce/attachment.htm>


More information about the erlang-questions mailing list