Julia Language Expressions Interpolation and Expressions

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

Creating Expressions mentions that expressions are closely related to strings. As such, the principles of interpolation within strings are also relevant for Expressions. For instance, in basic string interpolation, we can have something like:

n = 2
julia> MyString = "there are $n ducks"
"there are 2 ducks"

We use the $ sign to insert the value of n into the string. We can use the same technique with expressions. E.g.

a = 2
ex1 = :(x = 2*$a)  ##     :(x = 2 * 2)
a = 3
eval(ex1)
x # 4

Contrast this this:

a = 2
ex2 = :(x = 2*a) # :(x = 2a)
a = 3
eval(ex2)
x # 6

Thus, with the first example, we set in advance the value of a that will be used at the time that the expression is evaluated. With the second example, however, the Julia compiler will only look to a to find its value at the time of evaluation for our expression.



Got any Julia Language Question?