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
@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.
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]
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}])
do/endblocksThe last syntax convenience are
do/endblocks.do/endblocks 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 endis the same as:
if(true, do: (this), else: (that))
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
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.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









