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?

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

3 Likes

@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: https://hexdocs.pm/elixir/syntax-reference.html#syntactic-sugar

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: https://hexdocs.pm/elixir/syntax-reference.html#keywords

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: https://hexdocs.pm/elixir/syntax-reference.html#keywords-as-last-arguments

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: https://hexdocs.pm/elixir/syntax-reference.html#do-end-blocks

6 Likes

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.

1 Like