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 ...

Get Julia 1.0 Programming now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.