DidactMacros

DidactMacros

Understanding Elixir's Conversion of Blocks into Keyword Lists

In the context of iex…

…yields no error

IO.inspect :a do
5
end
#==> :a

…yields an error

:a do
5
end

error: syntax error before do

This question is in regards to the treatment of blocks in elixir, as I am trying to understand when they are automatically converted into kwlists.

Marked As Solved

zachallaun

zachallaun

They’re always converted to kw lists in contexts that allow kw lists. You can’t pass a kw list to the atom :a – it’s a syntax error.

For perhaps more clarity, the first example is equivalent to both of the following:

IO.inspect(:a, do: 5)
IO.inspect(:a, [{:do, 5}])

The :do is being passed as an option to the inspect function (and then ignored).

Also Liked

zachallaun

zachallaun

Here’s a handy guide on syntax sugar: Optional syntax sheet — Elixir v1.16.2

It’s definitely worth understanding. There’s surprisingly little of it!

DidactMacros

DidactMacros

That cleared up everything, especially the added clarification. Which explains why the block was ignored (not outputted) by the inspect.

Just to clarify, the macro tag is because I wanted to know if this conversion takes place even when internal representation is taking precedent (such as when passed as an arg to a macro), but this seems to happen before all that.

DidactMacros

DidactMacros

Thanks, was looking for something like this, but was searching under “blocks”.

More appreciation for the way the core team achieved elegance in elixir’s syntax.

sodapopcan

sodapopcan

It also might be useful to think of it in “reverse” as it’s a little more accurate: The do syntax is actually sugar. It’s really more: “when can do stand in for a :do key in a kwlist” if that makes sense. The “truer” syntax—for lack of a better word—is def foo, do: "foo", or rather def foo, [{:do, "foo"}].

I actually have a bit of trouble myself grokking how the def family of macros work because otherwise Elixir is really very lispish—it’s just keyword lists all the way down.

defmodule(Foo, [{:do, def(bar, [{:do, "foo"}])}])
sodapopcan

sodapopcan

I honestly don’t know! I haven’t never taken the time to fully understand at this level. defmacro defmacro calls define(:defmacro, ...) so AFAIK it’s the initial definition, at least in the Elixir part of Elixir’s source. Clearly there is something in the Erlang bits that are making that possible.

Where Next?

Popular in Questions Top

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
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
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
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New

We're in Beta

About us Mission Statement