Eval 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 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: a not defined. Only after we say, for example, a = 4 does eval(e2) and returns 12.
Expressions can also change the state of the execution ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access