[swift-evolution] [Discussion] Breaking precedence

Anton Zhilin antonyzhilin at gmail.com
Tue Aug 2 04:29:41 CDT 2016


2016-08-02 7:18 GMT+03:00 FĂ©lix Cloutier <swift-evolution at swift.org>:

> I disagree. The binary operators have properties that are comparable to
> arithmetic operators, and their precedence is easy to define as such. & has
> multiplication-like properties (0*0=0, 0*1=0, 1*0=0, 1*1=1); | has
> addition-like properties (0+0=0, 0+1=1, 1+0=1, 1+1=2); ^ has
> subtraction-like properties (0-0=0, 0-1=-1, 1-0=1, 1-1=0), and their
> precedences are set accordingly (& is multiplicative, | and ^ are additive).
>
> The same applies to && and ||. Bit shifts are exponentiative.
>

I believe that such way of thinking is non-intuitive. In C, bitwise
operators are not intervened by any others, except for comparison operators
(agreed, it was a mistake). We now have possibilities to do so in Swift,
even better. I suggest to branch off right before AdditionPrecedence:

RangeFormation < Addition < Multiplication
RangeFormation < BitwiseOr < BitwiseAnd < LogicalShift

Another concern is NilCoalescing, which can be though to be semantically
similar to Ternary. And at the same time it looks like || and &&, which
would bring it between LogicalConjunction and Comparison.
Also, do Casting and RangeFormation stand where they should?

Next, Ternary operator is unique. Noone would ever like to put operators in
this precedence group, because it would be confusing. Why not simplify our
model and say that ?: has lower precedence than all binary operators,
including Assignment? Unary > binary > ternary, sounds good?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160802/66b46409/attachment.html>


More information about the swift-evolution mailing list