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

Where Next?

Popular in Discussions Top

Other popular topics Top

josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39247 209
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35953 110
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I'm a nov...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

We're in Beta

About us Mission Statement