sodapopcan

sodapopcan

A little TIL regarding syntactic sugar

So this is just a dumb little post regarding syntax.

I’ve been pairing with a colleague who is new to Elixir lately and today we were briefly discussing how do/end is really syntactic sugar that essentially gets converted into a keyword list as [{:do, block}] and gets passed as the last argument to whatever is calling it. I was showing them how—while fairly useless—it even works with regular functions (not just macros) and I was creating modules with a single function in IEx to demonstrate. Afterwards this got me thinking if the following syntax would work:

foo = fn args ->
  args[:do]
end

foo.() do
  "bar"
end

Lo-and-behold it does!

Again, this is pretty useless in practice (and many of you are likely thinking “Sure, why wouldn’t that work?”) but it slightly expanded my own understanding of the ast/sugar ergonomics. Mostly I just thought it was cool and I’m home alone on a Friday night and I had to tell someone :upside_down_face:

If anyone has a better/different/(or even worse) way that they describe how do/end works, please feel free to share.

…and actually, are there practical uses of do/end for plain function functions I’m not thinking of?

Most Liked

gregvaughn

gregvaughn

All of those different clauses that seem like language keywords that you can pass to the def macro, such as rescue, catch, else, after can be used in that implicit last keyword list argument. I’m pretty sure the parser limits to those atoms, which means you can’t pick arbitrary ones, however, knowing how that works could open up possibilities of cool macro features.

Aside: Yes, “Mad About You” was a fun 90’s sitcom :grin:

BartOtten

BartOtten

I like the fact that you explored the “but wait, what if?” I never thought of exploring the option :sweat_smile:

BartOtten

BartOtten

I’m not the greatest communicator

I thought about disagreeing with your statement until.. :wink:

All is forgiven. You are so active in the community, I would not dare to be mad at you! Thanks for all your effort!

Fixed: “mad about you” is something else

Last Post!

a-maze-d

a-maze-d

Yes, that was exactly the initial plan to have something like

foo a: 1 bar
  # some code
end

Even though it does limit the DSL you can create from it. It does help to keep DSLs still somewhat consistent and I do think (in the meantime) that it’s a good thing :slight_smile:

Where Next?

Popular in Discussions Top

rower687
Hi all, I’ve been reading a lot about the “let it crash” term and how supervising processes and the whole messaging passing make an elixi...
New
pillaiindu
In django there is a cache framework backed by memcached. Rails also puts a lot of emphasis on caching, and even the idea of russian-doll...
New
acrolink
How does the two languages compare when it comes to server side application development? Any experiences or ideas? Thank you.
New
PragTob
Hello everyone, I know we had quite some threads (read through lots of them) about background job processing but it remains a hotly deba...
New
marciol
Please, let me know if this kind of discussion already took place in another topic . Hi all, how do you consider if is better to build ...
New
cvkmohan
The upcoming Phoenix 1.6 release looks very interesting. Became a habit to watch the commits - and - what they are bringing in. phx.gen...
New
matthias_toepp
I’d love to hear what people think about Wisp, the new Gleam web framework started by Gleam’s primary creator Louis Pilfold. Gleam, alon...
New

Other popular topics Top

grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54006 488
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31494 112
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement