[swift-evolution] [Discussion] Breaking precedence

Félix Cloutier felixcca at yahoo.ca
Mon Aug 1 23:18:02 CDT 2016


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.

Binary operators get especially confusing in some languages because their precedence is lower than comparison operators. For instance, in C, `a & b == c` gets parsed as `a & (b == c)`. In Swift, the precedence of binary operators is above that of comparison operators, so we don't have that problem.

Félix

> Le 1 août 2016 à 20:48:21, Rob Mayoff via swift-evolution <swift-evolution at swift.org> a écrit :
> 
>>> 1 | 2 ^ 3   // or this?
>> 
>> 
>> No. Both of those are bitwise operations. They are often used together. They
>> have a refined relative precedence in Swift that makes sense.
> 
> I have no idea what the relative precedence of those operators is in
> Swift, C, or any other language, and thinking about it now, no
> relative precedence seems sensible or obvious to me. Those operators
> (and bitwise-&) are excellent examples of operators that should
> require parentheses.
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160801/d1f47dc2/attachment.html>


More information about the swift-evolution mailing list