sodapopcan

sodapopcan

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?

Showing Posts 1 to 10

al2o3cr

al2o3cr

Personally, I’d find a do block passed to a plain function really confusing because I’ve written Ruby for years where that’s the syntax (more or less) for passing an anonymous function that the recipient can call.

Other keywords like else would be confusing to any Elixir developer, since they evaluate earlier than expected:

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

foo.() do
  IO.puts("something")
else
  IO.puts("something else")
end
sodapopcan

sodapopcan OP

Oh absolutely, which is why I said it was useless. But often when I say stuff like that people come back with an “aaaaccctually, in this one case…” but ya, I was pretty sure there was no good reason for it. I was just using it to illustrate to my coworker there is nothing particularly “special” with how do works.

BartOtten

BartOtten

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

sodapopcan

sodapopcan OP

I never said I didn’t think about it, I just couldn’t come up with anything worth sharing.

You could use it to make an over-engineered version of this:

foo = (
  bar = do_something()
  # ...
  bar
)
def cache(do: block), do: block

cache do
  bar = do_something()
  # ...
  bar
end

But foo = () is pretty useless as it is since variables leak so :person_shrugging:

D4no0

D4no0

This is very interesting, for some reason I always thought that having the do: block is special syntax that is only available in macros.

In some sense it is special as while the generated AST is just a keyword list, the tokenizer knows this is a special keyword.

sodapopcan

sodapopcan OP

I meant “not special beyond what is already special about it.” I’m not the greatest communicator :sweat_smile:

BartOtten

BartOtten

I never said you did say that. So I am a bit confused by the reply. Guess we are misaligned in a world of non-native English writers :writing_hand:.

sodapopcan

sodapopcan OP

Oh yes, I completely misunderstood what you were saying! Sorry about that!

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

sodapopcan

sodapopcan OP

Ohhhhh I re-read your comment and I just completely mis-read it. I’m definitely a bad reader. I read it as:

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

and was very confused.

edit: “mad about you” is something else

:sweat_smile:

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 91898 914
New
AstonJ
The obligatory hello world thread! Who are you and where are you from? :stuck_out_tongue:
4616 55835 594
New
byu
@chrismccord : I just saw the Extract AGENTS.md from Phoenix.new into phx.new generator commit to the phoenix project. My initial shotgu...
New
arcanemachine
I was working on an Ecto migration and I needed a timestamp. So, for the nth time, I looked up the different data types for timestamps, a...
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New
Herve37
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using. We’re particularly inte...
New
matt-savvy
Is there a word for the ~> symbol used in Version strings? Do you also just call it a Squiggle Arrow™ ?!
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews