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.20.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.

Where Next?

Popular in Questions Top

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
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
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

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29603 241
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36352 110
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement