Open hyrodium opened 11 months ago
This behavior is expected since the use of in
there is not a valid for
expression, even though it prints like one. Amending the expression slightly to make it evaluable at the top level, consider the following:
julia> ex1 = Expr(:block, Expr(:let, :(i = 0), Expr(:block, Expr(:for, :(j = 1:5), :(i += j)), :i)))
quote
let i = 0
for j = 1:5
i += j
end
i
end
end
julia> ex2 = Expr(:block, Expr(:let, :(i = 0), Expr(:block, Expr(:for, :(j in 1:5), :(i += j)), :i)))
quote
let i = 0
for j in 1:5
i += j
end
i
end
end
julia> ex3 = quote
let i = 0
for j in 1:5
i += j
end
i
end
end;
julia> Base.remove_linenums!(ex3)
quote
let i = 0
for j = 1:5
i += j
end
i
end
end
Note in particular how ex3
is printed: the loop specification uses =
even though ex3
was defined using in
. This is further exemplified by
julia> Meta.parse("for j in 1:5 end")
:(for j = 1:5
#= none:1 =#
#= none:1 =#
end)
And if we go to evaluate the expressions, we get
julia> eval(ex1)
15
julia> eval(ex3)
15
julia> eval(ex2)
ERROR: UndefVarError: `j` not defined in `Main`
Suggestion: check for spelling errors or missing imports.
Stacktrace:
[1] top-level scope
@ ./none:1
[2] eval
@ Core ./boot.jl:428 [inlined]
[3] eval(x::Expr)
@ Main ./sysimg.jl:48
[4] top-level scope
@ REPL[32]:1
This is because ex2
is defined incorrectly. Using in
is syntactic sugar that the parser turns into the correct expression, but when constructing an Expr
manually, you're bypassing that correction made by the parser.
MWE
The following functions
foo
andbar
should be identical, butbar
throws an error.Environment
I confirmed the above error was also reproduced on
v1.6.7
andv1.10.0-rc2
.Note
The
for x in X
syntax seems fine not in@generated
macro.