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

precedence of | and &



(Darn, I posted a previous confused verion of this, which I've tried to
cancel.  I don't know if I got it...)

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

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 right-associative when the DRM says they should be
left-associative.

In a chained expression such as...

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

... it makes no difference to the actual answer (well, unless I'm getting
really confused...) whether the operators are left-associative or
right-associative.  It seems to me that right-associative ...

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

... seem more natural because it implies that the exit occurs as soon as
it can.  If it was left-associative ...

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

... then it appears as if all the operators 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 the same as in Dylan.

Can anyone shed more light on this?


If & and | have the same precedence (as they do in Dylan) then I think
that right-associative (as Dylan isn't) is probably marginally 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



Follow-Ups: