New EEP draft: Pinning operator ^ in patterns

Peti Gömöri gomoripeti@REDACTED
Tue Jan 19 12:21:37 CET 2021


Hi

not wanting to convince anyone, just taking part in the opinion poll
I certainly hit the issue myself accidentally reusing a variable as well as
not spotting when reviewing code, and I would be fine to use the new pin
notation.
IDE support would help although it does not reflect the author's intent.

Only slightly related, got hit by the below using ms_transform in files

f1(Table, BoundKey) ->
    ets:select(Table, ets:fun2ms(fun({Key, Value}) when Key =:= BoundKey ->
Value end)).

f2(Table, BoundKey) ->
    ets:select(Table, ets:fun2ms(fun({BoundKey, Value})  -> Value end)).

The first function does a full table search while the second does a
straight key lookup (at least in the shell, in file BoundKey is obviously
shadowed). I find match-spec-functions much more readable but unfortunately
today there is no fun representation of
[{{BoundKey, '$2'}, [], ['$2']}]
When I saw the first post of Richard, it reminded me of this issue, that
the pin syntax could be used here too. Maybe this can be addressed in many
other ways, the ms_transform magic or the pattern-match-interpreter could
be made smarter.


On Mon, Jan 18, 2021 at 10:23 PM Raimo Niskanen <
raimo+erlang-questions@REDACTED> wrote:

> On Mon, Jan 18, 2021 at 07:22:21PM +0000, Matt Kowlaczyk wrote:
> > Jose Valim wrote a well written blog post titled "Comparing Elixir and
> > Erlang variables" [1] where he highlights the pin operator in Elixir and
> > explorers some of it's benefits. He succinctly addresses the class of
> > bugs of it catches and contrasts the operator with Erlang. Here is a
> > short snippet,
> >
> > > In other words, so far Elixir requires you to be mindful of all later
> > > code after the introduction of a variable while Erlang requires you to
> > > know all previous and further code before the introduction of a
> > > variable. The one benefit of Erlang so far is that the code may crash
> > > explicitly on the match.
> > >
> > > However, things get more complicated when considering case expressions.
> > >
> > Thought this would provide some useful context. It would be interesting
> > to see how the ideas in Jose's post reconcile under this proposed EEP. I
> > have not gone through the exercise.
> >
> > [1]
> >
> http://blog.plataformatec.com.br/2016/01/comparing-elixir-and-erlang-variables/
>
> As I read that blog post, most problems with the Erlang semantics that José
> lists would be remedied, if Erlang should have had a mandatory syntax for
> matching a variable.
>
> Most, but not for the problems with "Numbered variables", because Erlang
> does not have rebinding of variables.
>
> >
> >
> > --
> > Matt Kowalczyk
> >
>
> --
>
> / Raimo Niskanen, Erlang/OTP, Ericsson AB
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20210119/a348c186/attachment.htm>


More information about the erlang-questions mailing list