Macros
Armed with an understanding of symbols and expressions, we can now discuss creating macros.
A macro in Julia constructs boilerplate code by substituting its argument(s) after parsing and has no knowledge of the argument values. However, this is a great improvement to macros in (say) C/C++, which are essentially merely preprocessors prior to the parsing phase.
The first macro is very simple – if an expression is passed, it prints out its argument list; otherwise, it just returns:
# Check 'ex' is an expression, else just return it.julia>
macro pout(ex) if typeof(ex) == Expr println(ex.args) end return ex endjulia>
x = 1.1; @pout x 1.1 # For an expression return its arguments and then evaluate it.julia>
y = 2.3;julia>
@pout (x^2 + y^2 - 2*x*y)^0.5 Any[:^, :((x ^ 2 + y ^ 2) - 2 * x * y), 0.5] 1.1999999999999997
The following is a slightly more complex macro, which executes a body...