[swift-evolution] ? suffix for <, >, <=, >= comparisons with optionals to prevent subtle bugs
jtbandes at gmail.com
Thu Dec 3 18:17:26 CST 2015
Agreed — or, I think, these operators could/should simply be removed.
I filed <rdar://22833869> about this a while ago, which was marked as
> This returns true:
> (nil as Int?) < 0
> This also returns true, which makes even less sense:
> (nil as Int?) < Int.min
> nil < 0, nil > 0, and nil == 0, should all be false. nil != 0 should be true.
> Alternatively, just *don't* provide < and > operators that accept optional arguments.
> nil < 0 is true. Others are as expected.
See also: https://twitter.com/jtbandes/status/646914031433871364
On Thu, Dec 3, 2015 at 3:42 PM, Amir Michail <a.michail at me.com> wrote:
> Such comparisons with optionals can result in hard to find bugs.
> For example consider:
> let f = x < 5 // x is of type Int? and may be nil
> The proposed ? suffix would be used as follows and makes the possibility
> of nil very clear:
> let f = x? < 5
> swift-evolution mailing list
> swift-evolution at swift.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the swift-evolution