Julia Metaprogramming Tutorial
last modified November 18, 2026
Metaprogramming means writing programs that manipulate other programs. In Julia this is practical because code is represented as ordinary data: an expression is an object you can inspect, build and transform before it is evaluated. Macros are the best-known application of this idea.
This tutorial covers expressions, inspecting them with dump,
parsing source text with Meta.parse, QuoteNode,
interpolation, eval, and lowering with Meta.lower. The
examples were verified with Julia 1.13.4. Writing macros themselves is covered
in Julia macros.
Expressions
Prefixing Julia code with a colon creates an Expr object instead of
running it. An Expr is Julia's data structure for unevaluated
code; it has a head naming the operation and an args
array holding the operands.
ex = :(1 + 2) println(ex) println(typeof(ex)) println(ex.head) println(ex.args)
Printing the expression shows it in familiar source form, 1 + 2,
but its type is Expr. The head is the symbol
:call, meaning a function call, and the arguments are the function
+ followed by the two numbers. Holding code in this form is what
allows a program to rewrite it.
$ julia main.jl 1 + 2 Expr call Any[:+, 1, 2]
Inspecting Expressions with dump
The dump function prints the full tree structure of any value,
including nested expressions. This makes the recursive nature of Julia code
visible: one expression can contain other expressions as arguments.
dump(:(a + b * 2))
The outer expression is a call to + with two arguments. The first
is the symbol a, while the second is itself an expression: a call
to * with the symbol b and the integer 2. Each nested
level has its own head and argument list.
$ julia main.jl
Expr
head: Symbol call
args: Array{Any}((3,))
1: Symbol +
2: Symbol a
3: Expr
head: Symbol call
args: Array{Any}((3,))
1: Symbol *
2: Symbol b
3: Int64 2
Parsing Source with Meta.parse
The Meta.parse function turns a string of Julia source code into
an expression. It is the same parser the language uses when it reads a file,
so the resulting tree has exactly the structure you would expect from typed
code.
ex = Meta.parse("x = 1 + 2")
println(ex)
println(typeof(ex))
println(ex.head)
println(ex.args[1])
println(ex.args[2])
The parsed expression has the head =, the symbol that represents assignment.
Its first argument is the symbol x, the target of the assignment,
and its second argument is the expression 1 + 2. Parsing text is
the first stage in turning a string into executable code.
$ julia main.jl x = 1 + 2 Expr = x 1 + 2
QuoteNode
A QuoteNode marks a value that should be kept exactly as written
and not interpreted as code. It appears when an expression contains a quoted
symbol, and it prevents the symbol from being treated as a variable reference.
q = QuoteNode(:hello) println(q) println(typeof(q)) println(q.value)
The printed form is :hello, but the object is a
QuoteNode wrapping a symbol. Its value field holds the
symbol itself. Quote nodes are common in macro results where a bare symbol must
survive evaluation unchanged.
$ julia main.jl :hello QuoteNode hello
Expression Interpolation
Inside a quoted expression, a dollar sign inserts the current value of a variable. This is interpolation: the variable is evaluated now and its value becomes part of the expression, rather than the variable name remaining in the code.
x = 10 ex = :($x + 1) println(ex) println(typeof(ex.args[2])) println(ex.args[2])
Although the expression prints as 10 + 1, the value 10 was inserted
at construction time. The second argument is an Int64, not a
symbol named x. Interpolation is the main way to splice computed
values and other expressions into generated code.
$ julia main.jl 10 + 1 Int64 10
Evaluating Expressions
The eval function compiles and runs an expression in a module.
Passing a simple expression evaluates it immediately, and evaluating a parsed
assignment block creates bindings and returns the value of its last statement.
println(eval(:(2 + 3)))
ex = Meta.parse("a = 5; b = 7; a + b")
println(eval(ex))
The first call evaluates arithmetic and prints 5. The second parses three
statements separated by semicolons; evaluation assigns 5 and 7 and then
returns their sum, 12. Keep eval examples at the top level and
deterministic, because evaluation in the wrong module or at the wrong time can
behave surprisingly.
$ julia main.jl 5 12
Lowering with Meta.lower
Before Julia executes an expression, it lowers it: the surface syntax
is rewritten into a simpler internal form. The Meta.lower function
shows this intermediate representation, wrapped in a thunk that holds a
CodeInfo object.
lowered = Meta.lower(Main, :(1 + 2)) println(typeof(lowered)) println(lowered)
The result is an Expr whose contents reveal the lowered form of the
addition: Julia resolves + in Main as
Main.:+ and calls it with the two arguments. Lowering is a separate
stage from parsing and happens before type inference. The
InteractiveUtils standard library offers reflection macros such as
@code_lowered for exploring these stages interactively.
$ julia main.jl Expr $(Expr(:thunk, CodeInfo( 1 ─ %1 = Main.:+ │ %2 = dynamic (%1)(1, 2) └── return %2 )))
Building Expressions by Hand
Instead of parsing text you can construct an expression directly with the
Expr constructor. Its arguments are the head followed by the
operands, which is the same structure that :(...) produces.
ex = Expr(:call, :+, 1, 2) println(ex) println(eval(ex))
This builds a call to + with the integers 1 and 2, so printing it
shows 1 + 2 and evaluating it returns 3. Building expressions
programmatically is how code generators assemble code that did not exist as
source text.
$ julia main.jl 1 + 2 3
How Macros Use Expressions
A macro is a function that receives expressions as arguments and returns a new
expression. The macroexpand function shows the expression a macro
produces before it is evaluated, which makes the connection to metaprogramming
explicit.
macro twice(ex)
return :($ex + $ex)
end
println(macroexpand(Main, :(@twice(3))))
The macro body uses interpolation twice to insert its argument into both sides
of an addition. Expanding @twice(3) therefore yields
3 + 3. No addition happens during expansion; the returned
expression is evaluated afterwards. Macro hygiene, esc and the
built-in macros are explained in Julia macros.
$ julia main.jl 3 + 3
Best Practices for Metaprogramming
Generated code is harder to debug than ordinary code, so a few rules help.
- Inspect before evaluating: use
dumpormacroexpandto see the expression you have built. - Interpolate values, not strings: splice real values and
expressions with
$instead of concatenating source text. - Keep
evalsimple: prefer deterministic expressions and be aware of the module in which evaluation happens. - Use metaprogramming sparingly: plain functions are clearer when they can express the same behaviour.
Source
Julia Metaprogramming documentation
In this article we have met Julia metaprogramming: expressions and their
structure, dump, Meta.parse, QuoteNode,
interpolation, eval, Meta.lower and the connection to
macros. Macro writing is the subject of Julia
macros, and modules and evaluation scope are discussed in
Julia modules.
Author
List all Julia tutorials.