Should throw ERROR when if false?

julia> if false
           break
       end
ERROR: syntax: break or continue outside loop
Stacktrace:
 [1] top-level scope
   @ REPL[1]:1

I think this ERROR doesn’t have to be seen at REPL—it is distracting.

When the condition is false, we don’t actually care about the body of the if.
It should have no difference to the following—which has no ERROR

julia> quote
           break
       end
quote
    #= REPL[2]:2 =#
    break
end

Difference there is the code inside quote isn’t being evaluated…yet. If you do, you get the same error.

julia> eval(quote break end)
ERROR: syntax: break or continue outside loop

Evaluation of invalid Julia syntax throwing a syntax error is a lot better than mistakes silently doing something different from what the writer thought was possible.

We should. The condition being sometimes true in practice makes that obvious, but even if it were strictly if false, the variables in the body are involved in lexical scoping for consistency. A conditional branch that never executes doesn’t make dead code that can be trivially eliminated, and that’s true across many languages. Sometimes we have to just omit code we don’t need.

4 Likes