Defining macros
In previous chapters, we have already used macros, such as @printf
, in Chapter 2, Variables, Types, and Operations, and @time
in Chapter 3, Functions. Macros are like functions, but instead of values they take expressions (which can also be symbols or literals) as input arguments. When a macro is evaluated, the input expression is expanded, that is, the macro returns a modified expression. This expansion occurs at parse time when the syntax tree is being built, not when the code is actually executed.
Â
Â
The following descriptions highlight the difference between macros and functions when they are called or invoked:
- Function: It takes the input values and returns the computed values at runtime
- Macro: It takes the input expressions and returns the modified expressions at parse time
In other words, a macro is a custom program transformation. Macros are defined with the keyword as follows:
macro mname # code returning expression end
It is invoked as @mname exp1 exp2
or @mname(exp1...