Is there difference between Meta.eval and eval?

ex=quote
    x=1
    y=2
    x+y
end
ex.args
ex.head
Meta.eval(ex.args[6])
eval(ex.args[6])
julia> ex.head
:block

julia> Meta.eval(ex.args[6])
ERROR: UndefVarError: x not defined
Stacktrace:
 [1] top-level scope at none:0
 [2] eval at ./boot.jl:328 [inlined]
 [3] eval(::Expr) at ./meta.jl:6
 [4] top-level scope at none:0

julia> eval(ex.args[6])
3

I don’t understand

Every module has a function Module.eval that evaluates code in the scope of the module. x hasn’t been defined in the module Meta, so Meta.eval(ex.args[6]) throws an error. The reason why eval(ex.args[6]) works is probably because you defined x before in the same session. You can read more about how eval works here: https://docs.julialang.org/en/v1/manual/metaprogramming/index.html#QuoteNode-1

4 Likes