New EEP draft: Pinning operator ^ in patterns (zxq9)

Micael Nussbaumer micaelnussbaumer@REDACTED
Sat Jan 23 13:11:27 CET 2021


>>There are tons of ways to make sure that Erlang does not become Cobol,
and it
is all about tooling and libraries (do not get me started on calendar for
example), before touching the syntax.

>>Anyway, I don't expect any rush of people to suddenly use ZX -- that's
not the point. My point is that something *like* this is really the way
to make Erlang more adoptable, by changing the context of its use, not
changing the language itself.

I write Elixir, although I wrote a small lib in Erlang
https://github.com/mnussbaumer/workforce and I would like to write more
Erlang but, sincerely, those are the pain points and the lack of
metaprogramming (or tutorials on parse transforms lol).

I use the pinning operator regularly in Elixir - in actual code not that
much, I use it mostly in Ecto (because it's required) and occasionally to
force pattern matches or fails. I use it a lot in tests, because Elixir
allows shadowing/rebinding of variables, so in order to have a nice
readable assertion that is just written declaratively as a match, I need to
have that value pinned. This is a source of errors in the case of Elixir,
because of the rebinding that is allowed. Sometimes you can forget the
caret, and it will allow any match, and that part of the test will pass,
sometimes this means the test passes without really "passing" as I
intended. I'm now much more careful with it, but sincerely what I would
like in Elixir would be an option to just disallow rebinding and use
Erlang's single binding semantics.

By reading the long (and entertaining) thread about the proposal for the
introduction of this `operator` in Erlang I came to realize there's some
edge cases where the scoping rules overthrow the single binding semantics,
which seems unfortunate, although if that's solvable in some other way, I
think that would be the thing to actually solve - did I understand
correctly that in those samples shown here where shadowing is taking place,
the pinning operator would force a match when used, but if forgotten then
the variable would be rebound in that scope where it appears? Because
that's my main gripe with it in Elixir, the fact that if you forget to use
it, when you mean a match, then the variable, even though has been used
before is rebound and the match succeeds (which is normal, is the way
Elixir works).

This was a sidestep from what I wanted to reply to though,

The reason I don't write Erlang is because:
- I haven't found a tutorial or something explaining me how to use
parse_transforms - I toyed with the idea of writing a HTTP server as a
learning endeavour - I actually wrote the basics of it up to parsing and
actually replying (HTTP v1 ofc only) in Erlang before going for Elixir (and
I was using Elixir already for a long time at this point). I wanted to
write a friggin' router system, similar to phoenix (and a lot of other
frameworks out there), where you specify your routes as lines. I ended up
having to write a small parser that took those lines, parsed them, then
wrote them as if plain code with those bits that I parsed, into a file,
that then I compiled in order to have a dynamic thing (and I had to use a
lib that I found somewhere, that was someone's experiment with dynamic
compilation because I couldn't figure out how to do it). In Elixir I wrote
a macro, that I think couldn't really be more readable than what it is that
does that at compile time, in a clean way. I understand that
metaprogramming can be a curse, so I wouldn't say add fully fledged
metaprogramming capabilities to erlang,  but some resources on how to
achieve those things in Erlang with what is there already,
parse_transforms, would go a long way. And I'm someone who almost never
writes macros but does use metaprogramming in some way or another.

- Releases/Assembling a Runnable program - I only use releases in Elixir, I
don't understand most of what happens in there except at a high level
though. Here I think rebar has it covered? I never tried to build a release
in erlang proper I think. Again, I think tutorials would be great, but not
the put rebar and run this command style, instead on how to do what rebar
is doing - "give a man a fish..."

- The syntax is fine - with some editor help (but that's the same with any
other language sincerely) it's pretty readable - it looked a bit alien at
first, but afterwards it actually looks good.

- The single binding (Except the shadowing) is good, I use the same *_1,
*_2 styling in Elixir too now (but I don't have the errors when I don't,
which I miss) simply because it's less error prone at the cost of a bit
more occasional rewriting (and actually the only place I see regular need
for this is in tests, most of the normal code doesn't see this happening or
be needed all that often).

Anyway, since this thread has been so entertaining I thought I would chip
in with my two unasked for
cents,

regards,
*M*icael *N*ussbaumer
artistic portfolio <http://cargocollective.com/micaelnussbaumer>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20210123/d75f4b70/attachment.htm>


More information about the erlang-questions mailing list