Julia LanguageMetaprogramming


Syntax

  • macro name(ex) ... end
  • quote ... end
  • :(...)
  • $x
  • Meta.quot(x)
  • QuoteNode(x)
  • esc(x)

Remarks

Julia’s metaprogramming features are heavily inspired by those of Lisp-like languages, and will seem familiar to those with some Lisp background. Metaprogramming is very powerful. When used correctly, it can lead to more concise and readable code.

The quote ... end is quasiquote syntax. Instead of the expressions within being evaluated, they are simply parsed. The value of the quote ... end expression is the resulting Abstract Syntax Tree (AST).

The :(...) syntax is similar to the quote ... end syntax, but it is more lightweight. This syntax is more concise than quote ... end.

Inside a quasiquote, the $ operator is special and interpolates its argument into the AST. The argument is expected to be an expression which is spliced directly into the AST.

The Meta.quot(x) function quotes its argument. This is often useful in combination with using $ for interpolation, as it allows expressions and symbols to be spliced literally into the AST.