带有相等运算符的Julia表达式不起作用



以下代码按预期工作:

x = 1
exp = Expr(:(=), :x, 4)   # :(x = 4)
eval(exp)                 # x is now equal to 4 as expected

以下代码失败:

x = 1
exp = Expr(:(==), :x, 4)  # Got :($(Expr(:(==), :x, 4))) instead of the expected :(x == 4)
eval(exp)                 # ERROR: syntax: invalid syntax (== (outerref x) 4)

==是一个函数,因此您有:

julia> dump(:(x==4))
Expr
head: Symbol call
args: Array{Any}((3,))
1: Symbol ==
2: Symbol x
3: Int64 4

但是

julia> dump(:(x=4))
Expr
head: Symbol =
args: Array{Any}((2,))
1: Symbol x
2: Int64 4

因此,特别是以下工作:

julia> x = 1
1
julia> exp = Expr(:call, :(==), :x, 4)
:(x == 4)
julia> dump(exp)
Expr
head: Symbol call
args: Array{Any}((3,))
1: Symbol ==
2: Symbol x
3: Int64 4
julia> eval(exp)
false

相关内容

  • 没有找到相关文章

最新更新