[Prev][Next][Index][Thread]

precedence of | and &



The DRM (and other sources) state that the short-circuit boolean oerators,
| and &, have the same precedence and are right-associative (like ^ and :=
and the opposite of everything else).

The folly of relying on precedence in complicated expressions aside, does
anyone else think that it would really be better if | had lower precedence
than &, as in practicaly every other language in common use?


I also wonder about the associativity.  Andreas just fixed a bug in d2c
where | and & were left-associative when the DRM says they should be
right-associative.
In a chained expression such as...

   a | b | c | d
or
   a & b & c & d

... it make no difference to the actual answer (well, unless I'm getting
really confused...) whether the operators are left-associative or
right-associative and right-associative ...

a | (b | (c | d))
a & (b & (c & d))

... seem more natural to me because they imply that the xit occirs as soon
as it can.  If it was left-associative ...

((a | b) | c) | d
((a & b) & c) & d

... then it appears as if alll the opertors have to be evaluated even
though the result is the same.

Strangely, though, in both C/C++ and Perl the && and || (as well as the
lower-than-assignment-precedence "and" and "or" in Perl) are specified as
*left* associative.


If & and | have the same precedence (as they do in Dylan) then I think
that right-associative (as Dylan is) is probably the more intuitive choice
in expressions which mix both operators together.  But it seems to me that
it's a choice between two pretty poor alternatives, and that we'd be
better off to give & and | different precedences.

No doubt any S-expression fans will be laughing their heads off at this point...

-- Bruce