引号表达式中的Julia全局变量引发UndervarError

引号表达式中的Julia全局变量引发UndervarError,julia,expression,eval,metaprogramming,Julia,Expression,Eval,Metaprogramming,以下代码失败,并显示一条有意义的错误消息 x = 10 exp = quote for i in 1:10 x = x + 1 end x end eval(exp) ┌ Warning: Assignment to `x` in soft scope is ambiguous because a global variable by the same name exists: `x` will

以下代码失败,并显示一条有意义的错误消息

x = 10
exp = quote
          for i in 1:10
              x = x + 1
          end
          x
      end
eval(exp)

┌ Warning: Assignment to `x` in soft scope is ambiguous because a global variable by the same name exists: `x` will be treated as a new local. Disambiguate by using `local x` to suppress this warning or `global x` to assign to the existing global variable.
└ @ REPL[177]:3
ERROR: UndefVarError: x not defined
Stacktrace:
 [1] top-level scope
   @ ./REPL[177]:3
 [2] eval
   @ ./boot.jl:360 [inlined]
 [3] eval(x::Expr)
   @ Base.MainInclude ./client.jl:446
 [4] top-level scope
将x的作用域声明为本地可使事情正常工作:

exp = quote
          local x = 0
          for i in 1:10
              x = x + 1
          end
          x
      end
eval(exp)   # 10 as expected
      
但是,由于某些原因,这不起作用:

x = 0
exp = quote
          global x
          for i in 1:10
              x = x + 1
          end
          x
      end
eval(exp)

ERROR: UndefVarError: x not defined
Stacktrace:
 [1] top-level scope
   @ ./REPL[182]:4
 [2] eval
   @ ./boot.jl:360 [inlined]
 [3] eval(x::Expr)
   @ Base.MainInclude ./client.jl:446
 [4] top-level scope

更改
全局的位置

julia> x=0;

julia> exp2 = quote
                 for i in 1:10
                     global x = x + 1
                 end
                 x
             end;

julia> eval(exp2)
10

非常感谢!但是为什么它和本地人一起工作呢?这两个人做的事情非常不同<代码>局部在您创建的代码块中引入新的局部变量<代码>全局说明如何使用变量。