sodapopcan
I’m wondering exactly what a do block is.
Is it a closure? Is it a procedure? Is it an anonymous function? Is it a lambda? Is it something else?
Is it more useful to think of an Elixir do block as a Ruby block or as Haskell’s do notation or maybe a combination of the two? Or neither!!? Maybe even a JS closure?
Is it some mutant variation of all of that?? Or maybe none of that!???
I do (haha, get it?) have some intuition about this and have read the meta-programming part of the Elixir guides (which calls it an expression but, like, it’s really a series of expressions… no?) and I’m just looking for some insightful—or even just fun—responses from people in the community, if anyone is willing.
What I already think I know:
dois syntactic sugar for calling the, uh, “block of code” provided to the:dokey of a keyword list arg provided to a macro- That’s it!
Thank you!!
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted”
Version...
New
Other Trending Topics
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
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #security










Showing Posts 1 to 6- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
ityonemo
it’s actually a lisp-ish AST construct.
you can see this most clearly by doing this:
which gives you this:
and
that’s why sometimes most constructs let you write
, do:with the colon as shorthand, because that’s exactly what was there!ityonemo
haha maybe I think I should break this down more carefully. A macro in elixir is a function that emits AST + “take the AST generated, right here in the code” (a bare function will drop its value, in that location, not the AST). So macros in the first are a function. Let’s consider the “def” macro.
We’ll look at it three ways:
which yields:
and
which yields
and
which yields
Note a) forms 1 and 2 are identical
and b) form 3 groups the two lines of the function into a block.
Why is that necessary? Because according to Kernel.def/2 documentation (Kernel — Elixir v1.20.2), the function half of the
defmacro must take TWO AST parameters. The first parameter is the name of the function (and also a list of arguments) and the second parameter is the contents of the function. So the only arity-consistent way of representing def is by grouping the AST for the body of the function into a block and labeling it as “do”. So the do “keyword” is just elixir’s cute way of saying "hey everything until the “end” keyword winds up in the block. And if you don’t want a block, you can just pass it asdo:, which builds the keyword list manually and passes it into the def function without syntactic sugar.One more important thing to note is that in elixir, lists, atoms, and two-tuples don’t need escaping, because they are identical to their AST, so that’s how when you do a one-liner the
do:part of the code makes it into the macro without being converted into these strange tuple forms.as for why it’s a keyword list, if you look further down the documentation, you can have other parts to the function code, for example
catchorrescueblocks; if you do that, for example this code which is nonsensical:yields:
so other “segments” of the function can nicely wind up stuffed into an arity-2 statement. Likewise you can arbitrarily mix-and-match
catches andrescues, because a keyword-list is what you are supposed to use when you have a maybe-ordered list of arbitrary things that can’t be a map because you can maybe have duplicates.josevalim
In a nutshell,
do-endblocks is a syntax sugar for a:dokeyword list.is the same as:
You can see this by calling
quoteas shown by @ityonemo. Since keyword lists are just data structures, do-blocks end-up being just data structures too. It doesn’t need to be given only to macros, regular functions can also use and match on it, some helpers in Phoenix.HTML do so, but their usage is indeed more common with macros.As a syntax sugar, they also allow some specific syntactical constructs, such as
left -> rightand other keywords (else, rescue, etc), but all of them can be written in the literal keyword syntax format too.ityonemo
Just tried it. Mind blown. I mean, of course, what was I thinking, why wouldn’t that work??!. I promise not to use this power for evil.
sodapopcan
Thank you both for your answers!
As stated, I am well aware of it being just syntactic sugar for a
:doin a keyword list, but these answers were still very helpful. I never thought tounquotethem! I was also unaware that the:dosyntax would work for functions too… not sure why I just didn’t try it…Thanks especially @ityonemo for your very detailed response… there was some good insight there that is helping me grok this.
I was really mainly looking to understand what they are conceptually as related to concepts in other languages (for example how a ruby block is closure) although I’m starting to see that they really are kinda “just blocks of code” and it all depends on how the macro or function decides to deal with them. I was going to do some tests before responding buuuut I felt like responding and will play around with it later.
Thanks again!
vfsoraki
I always think of
dolikeprognis LISP.