9 - Metaprogramming
Previous8 - Interfacing Julia with other languagesNext10 - Performance (parallelisation, debugging, profiling..)
Last updated
expr = Meta.parse("1+2") # parses the string "1+2" and saves the `1+2` expression in the `expr` expression, same as expr = :(1+2)
eval(expr) # here the expression is evaluated and the code returns 3a = 2;
ex = Expr(:call, :*, a, :b) # ex is equal to :(2 * b). Note that b doesn't even need to be defined
a = 0; b = 2; # no matter what now happens to a, as a is evaluated at the moment of creating the expression and the expression stores its value, without any more reference to the variable
eval(ex) # returns 4, not 0macro unless(test_expr, branch_expr)
quote
if !$test_expr
$branch_expr
end
end
endarray = [1, 2, 'b']
@unless 3 in array println("array does not contain 3") # here test_expr is "3 in array" and branch_expr is "println("array does not contain 3")"if !(3 in array)
println("array does not contain 3")
end