Evaluation and interpolation
With the definition of type Expr
from the preceding section, we can also build expressions directly from the constructor for Expr
. For example: e1 = Expr(:call, *, 3, 4)
returns :((*)(3, 4))
(follow along with the code in Chapter 7\eval.jl
).
The result of an expression can be computed with the eval
function, eval(e1)
, which returns 12
in this case. At the time an expression is constructed, not all the symbols have to be defined, but they have to be defined at the time of evaluation, otherwise an error occurs.
For example, e2 = Expr(:call, *, 3, :a)
returns :((*)(3, a))
, and eval(e2)
 then gives ERROR: UndefVarError: a not defined
. Only after we say, for example, a = 4
does eval(e2)
 return 12
.
Expressions can also change the state of the execution environment, for example, the expression e3 = :(b = 1)
assigns a value to b
when evaluated, and even defines b
, if it doesn't exist already.
Â
To make writing expressions a bit simpler, we can use the $
operator to do interpolation...