December 2018
Beginner to intermediate
500 pages
12h 10m
English
Now, we finally have the knowledge to understand macros. They are language constructs, which are executed after the code is parsed, but before it is evaluated. It can optionally accept a tuple of arguments and must return an Expr. The resulting Expression is directly compiled, so we don't need to call eval() on it.
For example, we can implement a configurable version of the previous greet expression as a macro:
julia> macro greet(name)
:("Hello " * $name)
end
@greet (macro with 1 method)julia> @greet("Adrian")
"Hello Adrian"
As per the snippet, macros are defined using the macro keyword and are invoked using the @... syntax. The brackets are optional when invoking macros, so we could also use @greet "Adrian".
Macros are very powerful ...
Read now
Unlock full access