[swift-evolution] Nil coalescing operator precedence

Xiaodi Wu xiaodi.wu at gmail.com
Tue Jun 14 18:21:30 CDT 2016


This is an orthogonal issue to the first that you identified. The first has
to do with the associativity of the operator, the second has to do with the
precedence of the operator.

I think there's sufficient confusion about the first that it's worth
addressing. However, with respect to precedence, at the end of the day
every operator has to have a precedence. Since ?? and ?: have no
counterparts in math, there's no reason that a new user would know whether
the precedence is higher or lower than that of arithmetic operators. It's
simply listed in a table.

I don't know that it's feasible to warn on every use of operators with
mixed precedence. Doing so would effectively do away with the concept of
precedence at all, since everything would have to be grouped by parentheses
in order to avoid warnings. (The core team has been pretty clear that there
will be no 'optional' warnings, and in certain organizations warnings are
regarded as errors.)
On Tue, Jun 14, 2016 at 17:50 Andrey Fidrya via swift-evolution <
swift-evolution at swift.org> wrote:

> I've given it some more thought... Even expressions with single ?? can be
> confusing.
> For example:
>
> 1)
>
> let z = a ?? x + y + z
>
> Actually it's
> let z = a ?? (x + y + z)
>
> But can be mistakenly interpreted as
> let z = (a ?? x) + y + z
>
> 2) Same problem with ?:
>
> let z = a ? b : c + x + y
>
> It's
> let z = a ? b : (c + x + y)
>
> Not
> let z = (a ? b : c) + x + y
>
> Possibly warnings should be shown in both these cases. Or is it too
> extreme?
>
> --
> Andrey Fidrya
>
>
>
> On 15 Jun 2016, at 01:08, Roth Michaels via swift-evolution <
> swift-evolution at swift.org> wrote:
>
> On Sun, Jun 12 2016 at 02:01:17 AM, Andrey Fidrya via swift-evolution <
> swift-evolution at swift.org> wrote:
>
> Nil coalescing operator has very low precedence and it's very easy to
> forget the parentheses.
> It's tempting to write something like this:
>
> let result = v1 ?? 0 + v2 ?? 0
>
> Which will resolve to
>
> let result = v1 ?? (0 + v2 ?? 0)
>
>
> This is how I would expected the operator to work.
>
> This seems to be a source of errors in other languages as well, for
> example:
>
> http://www.codeproject.com/Tips/721145/Beware-The-null-coalescing-operator-is-low-in-the
>
> I propose to consider raising it's precedence or requiring parentheses if
> ?? is used with multiple statements.
>
>
> I like the idea of requiring parenthesis (or at least having a warning)
> on ambiguous lines with two `??`.
>
> --
> Roth
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
>
> _______________________________________________
> 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/20160614/6ea49e88/attachment.html>


More information about the swift-evolution mailing list