mcgingras

mcgingras

What is the "do" in a defmacro param?

Hello all,

I’ve been reading about metaprogramming and encountered examples that look like the following:
defmodule example(example_param, do: block)

I haven’t encountered syntax with the do param like that before, and I’ve been confused how that works. I have not been able to find much in the way of documentation since quite honestly I’m not even sure what to search for.

How does the caller pass the “block” to the function? What is this sort of syntax called?

Most Liked

Eiji

Eiji

@mcgingras If you do not understand some syntax you should check Syntax reference special documentation page.

Regarding your questions you should be especially interested in those sections:

Syntactic sugar

All of the constructs above are part of Elixir’s syntax and have their own representation as part of the Elixir AST. This section will discuss the remaining constructs that “desugar” to one of the constructs explored above. In other words, the constructs below can be represented in more than one way in your Elixir code and retain AST equivalence.

Source: Syntax reference — Elixir v1.16.0

Keywords

Keywords in Elixir are a list of tuples of two elements where the first element is an atom. Using the base constructs, they would be represented as:

[{:foo, 1}, {:bar, 2}]

However Elixir introduces a syntax sugar where the keywords above may be written as follows:

[foo: 1, bar: 2]

Source: Syntax reference — Elixir v1.16.0

Keywords as last arguments

Elixir also supports a syntax where if the last argument of a call is a keyword list then the square brackets can be skipped. This means that the following:

if(condition, do: this, else: that)

is the same as

if(condition, [do: this, else: that])

which in turn is the same as

if(condition, [{:do, this}, {:else, that}])

Source: Syntax reference — Elixir v1.16.0

do/end blocks

The last syntax convenience are do/end blocks. do/end blocks are equivalent to keywords as the last argument of a function call where the block contents are wrapped in parentheses. For example:

if true do
  this
else
  that
end

is the same as:

if(true, do: (this), else: (that))

Source: Syntax reference — Elixir v1.16.0

RudManusachi

RudManusachi

It’s nothing but the keyword list with key :do. The equivalent would be:
defmodule example(example_param, [{:do, block}])

And usually inside the macro you unquote the block.

if the last argument of a call is a keyword list then the square brackets can be skipped

See keywords as last arguments, followed by the section about do/end blocks

The other thing is elixir’s tokenizer treats do/end as an “expression” where end is a terminal “end of expression”.

mcgingras

mcgingras

Thank you! I wasn’t sure what to search within the reference material. “Keywords as last arguments” is exactly what I am looking for. Thanks so much for the help.

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
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
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
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
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics 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
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
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
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
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
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New

We're in Beta

About us Mission Statement