[swift-evolution] Nil coalescing operator precedence

Roth Michaels roth at rothmichaels.us
Tue Jun 14 17:08:08 CDT 2016


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



More information about the swift-evolution mailing list