Open
Description
julia> if true println("hello")
1
end
hello
1
julia> Meta.@lower if true println("hello")
1
end
:($(Expr(:thunk, CodeInfo(
@ none within `top-level scope`
1 ─ goto #3 if not true
@ REPL[13]:1 within `top-level scope`
2 ─ println("hello")
│ @ REPL[13]:2 within `top-level scope`
└── return 1
3 ─ return nothing
))))
The println("hello")
expression becomes part of the body of the true branch. This "feature" is even more nasty when you do by mistake something like
if condition1 && condition2 condition3 && condition4
condition3 && condition4
aren't evaluated at all as part of the conditions of the if
. This should arguably be a parser/syntax error. For example
julia> if true 1 2 3
1
end
ERROR: syntax: unexpected "2"
Stacktrace:
[1] top-level scope
@ none:1
2
is a syntax error, I don't see why 1
shouldn't.
Other weird interpretations of this "feature":
julia> if true :foo
1
end
ERROR: UndefVarError: `foo` not defined
Stacktrace:
[1] top-level scope
@ REPL[15]:1
julia> if true :2
1
end
ERROR: TypeError: non-boolean (UnitRange{Int64}) used in boolean context
Stacktrace:
[1] top-level scope
@ REPL[17]:1