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?
@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.
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:
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:
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.