There are a number of different methods that can be used to create the same type of expression. The expressions intro mentioned the :()
syntax. Perhaps the best place to start, however is with strings. This helps to reveal some of the fundamental similarities between expressions and strings in Julia.
Create Expression from String
From the Julia documentation:
Every Julia program starts life as a string
In other words, any Julia script is simply written in a text file, which is nothing but a string of characters. Likewise, any Julia command entered into an interpreter is just a string of characters. The role of Julia or any other programming language then is to interpret and evaluate strings of characters in a logical, predictable way so that those strings of characters can be used to describe what the programmer wants the computer to accomplish.
Thus, one way to create an expression is to use the parse()
function as applied to a string. The following expression, once it is evaluated, will assign the value of 2 to the symbol x
.
MyStr = "x = 2"
MyExpr = parse(MyStr)
julia> x
ERROR: UndefVarError: x not defined
eval(MyExpr)
julia> x
2
Create Expression Using :()
Syntax
MyExpr2 = :(x = 2)
julia> MyExpr == MyExpr2
true
Note that with this syntax, Julia will automatically treat the names of objects as referring to symbols. We can see this if we look at the args
of the expression. (See Fields of Expression Objects for more details on the args
field in an expression.)
julia> MyExpr2.args
2-element Array{Any,1}:
:x
2
Create Expression using the Expr()
Function
MyExpr3 = Expr(:(=), :x, 2)
MyExpr3 == MyExpr
This syntax is based on prefix notation. In other words, the first argument of the specified to the Expr()
function is the head
or prefix. The remaining are the arguments
of the expression. The head
determines what operations will be performed on the arguments.
For more details on this, see Fields of Expression Objects
When using this syntax, it is important to distinguish between using objects and symbols for objects. For instance, in the above example, the expression assigns the value of 2
to the symbol :x
, a perfectly sensible operation. If we used x
itself in an expression such as that, we would get the nonsensical result:
julia> Expr(:(=), x, 5)
:(2 = 5)
Similarly, if we examine the args
we see:
julia> Expr(:(=), x, 5).args
2-element Array{Any,1}:
2
5
Thus, the Expr()
function does not perform the same automatic transformation into symbols as the :()
syntax for creating expressions.
Create multi-line Expressions using quote...end
MyQuote =
quote
x = 2
y = 3
end
julia> typeof(MyQuote)
Expr
Note that with quote...end
we can create expressions that contain other expressions in their args
field:
julia> typeof(MyQuote.args[2])
Expr
See Fields of Expression Objects for more on this args
field.
More on Creating Expressions
This Example just gives the basics for creating expressions. See also, for example, Interpolation and Expressions and Fields of Expression Objects for more information on creating more complex and advanced expressions.