Book Image

Getting Started with Julia

By : Ivo Balbaert
Book Image

Getting Started with Julia

By: Ivo Balbaert

Overview of this book

Table of Contents (19 chapters)
Getting Started with Julia
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
The Rationale for Julia
Index

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 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 in expressions;...