The Eval function
The eval()
function is simply used for executing or evaluating an Expression
object. The evaluations and executions are done in a global scope.
Getting ready
To get started with this section, you must simply have your Julia REPL up and running.
How to do it...
Let's work on some examples to understand the eval()
function better.
Construct an expression for adding two variables:
First define the variable:
p = 2 q = 3
The output would look like this:
Now, construct the expression:
exp = :(p + q)
Now, check the value of the expression with the
eval()
function:eval(exp)
Now, let's look at functions that take in one or more Expression
objects as input arguments and return another Expression
object as the output. Let's understand this better through an example:
The following code creates a function that we discussed in the preceding example, one which takes in expressions as inputs and also return expressions as outputs:
function example_exp(op, var1, var2) exp = Expr(:call, op...