[swift-evolution] Standard operator precedence

Xiaodi Wu xiaodi.wu at gmail.com
Sun Feb 14 12:25:08 CST 2016

Some further study has been helpful. Am I close to the mark in answering my
own question?

- Further reading shows that Swift's rationalized operator precedence
levels broadly align with those of Erlang and Go. (However, those languages
also seem to lack documentation on how they arrived at this set of
precedence levels.)

- It is quite evident why << is considered exponentiative.

- Dennis Ritchie has explained <http://www.lysator.liu.se/c/dmr-on-or.html>
why & has lower precedence than == in C, and why in hindsight that is
better off changed. This change has been implemented in Swift/Go/Erlang and
also in other languages like Python.

- & is considered multiplicative because for two bits A and B, A & B == A *

- ^ and | should have equal precedence to - and +, respectively, by
analogous reasoning.

- It also happens to be rational for & to have higher precedence than | by
analogy with && and ||.

It's a bit of a bummer, if this line of reasoning was indeed used in
arriving at these operator precedence levels, that Swift will not allow
evaluation of true * true (etc.) without casting. I would expect true &
true == true * true.

On Sun, Feb 14, 2016 at 1:42 AM Xiaodi Wu <xiaodi.wu at gmail.com> wrote:

> Very well. May I ask what the rationale might be? I'm not sure I can
> deduce it from the available documentation. In other circumstances where
> Swift operators differ from C (e.g. overflow handling for addition, etc.),
> the behavior is amply documented and the rationale quite clear. (I should
> add that Swift documentation currently introduces these bitwise operators
> as 'familiar' to those coming from C and Objective-C before defining what
> they are, which discourages careful study.)
> On Sat, Feb 13, 2016 at 10:10 PM Chris Lattner <clattner at apple.com> wrote:
>> > On Feb 13, 2016, at 6:32 AM, Xiaodi Wu via swift-evolution <
>> swift-evolution at swift.org> wrote:
>> >
>> > Not sure if this is intentional, a bug, and/or a topic for evolution:
>> >
>> > In Swift, bitwise operators seem to have a different precedence in
>> > relation to other operators than they do in (all other?) C-family
>> > languages, at least per documentation.
>> Yep, this is true, and this is intentional.  Swift has a greatly
>> simplified and rationalized set of precedences, and yes, that means they
>> differ from C.
>> -Chris
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160214/db4aacb6/attachment.html>

More information about the swift-evolution mailing list