• Subject: Re: Ternary operator patch
• From: Tony Finch <dot@...>
• Date: Thu, 16 Sep 2010 16:37:57 +0100

```On Thu, 16 Sep 2010, David Given wrote:
>
> But what's the issue with & and |? & has a higher priority than |, which
> is what I'd expect (boolean multiply vs boolean add).

The problem is their precedence relative to the comparison operators.

if (a & 15 == 0) fail();

In a better world C's precedence table would be:

() [] -> . ++ --                     left to right
- ! ~ ++ -- * & (type) sizeof        right to left
* / % & << >>                        left to right
+ - ^ |                              left to right
< <= > >= == !=                      left to right
&&                                   left to right
||                                   left to right
?:                                   right to left
= += -= *= /= %= <<= >>= &= ^= |=    right to left
,                                    left to right

Even better would be to replace prefix * with postfix ^ (like Pascal)
which would fix the tortured declaration syntax. Postfix ^ can be optional
before . just like it is before () and [], so you can abolish ->. Infix ^
would need to be spelled differently to avoid a clash; ~ would make sense
by analogy with -. It should also be possible to chain comparison
operators like BCPL and Python. Then you would get:

() [] ^ . ++ --                      left to right
- ! ~ ++ -- & (type) sizeof          right to left
* / % & << >>                        left to right
+ - ~ |                              left to right
< <= > >= == !=                      chained
&&                                   left to right
||                                   left to right
?:                                   right to left
= += -= *= /= %= <<= >>= &= ^= |=    right to left
,                                    left to right

Tony.
--
f.anthony.n.finch  <dot@dotat.at>  http://dotat.at/
HUMBER THAMES DOVER WIGHT PORTLAND: NORTH BACKING WEST OR NORTHWEST, 5 TO 7,
DECREASING 4 OR 5, OCCASIONALLY 6 LATER IN HUMBER AND THAMES. MODERATE OR
ROUGH. RAIN THEN FAIR. GOOD.

```