-
Notifications
You must be signed in to change notification settings - Fork 0
Metaprogramming
Julia code is represented as a syntax tree built out of Julia data structures of type Expr. This makes it easy to construct and manipulate Julia code from within Julia, without generating or parsing source text. Here is the definition of Expr:
type Expr
head::Symbol
args::Array{Any,1}
typ
end
The head is a symbol identifying the kind of expression, and args is an array of subexpressions. typ is used by type inference to store type annotations (it can generally be ignored).
There is special syntax for "quoting" code (analogous to quoting strings) that makes it easy to create expression objects without explicitly constructing Exprs. There is a long form, for blocks of code, using quote ... end, and a short form using a prefix :.
julia> e = :(a+b)
+(a,b)
julia> e.head
call
julia> e.args
{+,a,b}
Values of expressions may be interpolated into quote expressions with $ as in strings:
julia> a=1
1
julia> e = :($a+b)
+(1,b)
Previous: Parallel Computing — Next: Calling C Code