I’ve found myself unable to explain the output of the following simple Julia program. It appears the output is totally dependent on whether one applies the short-circuiting rules or the precedence rules first. Short-circuiting is a minor optimization for a couple of operators whereas precedence affects all operators. Yet Julia seems to treat short-circuiting as the more important of the two. An example:
# Apparent conflict between precedence and short-circuit evaluation # First define some helper functions t(x)=(println(x);true) f(x)=(println(x);false) # Now comes the critcal expression t(1) || f(2) && f(3) # If one starts with the short-circuit rules, one would argue that || doesn't # need to evaluate f(2) && f(3), because t(1) is true, giving an output # of 1 \n true. By actual experiment this is what Julia does. # However, one could start with the precedence rules. && has higher precedence, # so it is evaluated first. The fact that || doesn't need the result of && is # irrelevant because we aren't evaluating ||. f(2) ie evaluated and then by # the short-circuiting of &&, f(3) is ignored. So 2 \n is outputted and # false is returned. || now is the only remaining operator, so t(1) is # evaluated, causing the output now to be 2 \n 1 \n and returning true. Now # the short-circuit rules claim we should ignore the value of && and return true # as the final value. # Julia apparently applies short-circuit before precedence. Why??