[swift-evolution] [Review] SE-0121: Remove `Optional` Comparison Operators

Mike Sanderson m at mikesand.com
Mon Jul 18 01:14:41 CDT 2016

>         * What is your evaluation of the proposal?

+1. Agree the comparison can be surprising, and it also confuses some
people trying to understand optionals, apparently; I once couldn't convince
someone how it worked until shortly afterwards for another issue @jckarter
phrased it "Optional defines an ordering for itself with nil < everything."
(https://twitter.com/jckarter/status/657266671703359488 ) That Optional
defines an ordering for itself also emphasizes that nil < everything is

I think the question of the proposal is exactly "what remains is to decide
whether these semantics (that nil is "less than" any non-nil value) are
actually useful and worth keeping."

The Pet example shows how an asymmetry is caused: If someone wanted to
filter people who don't have pets under 6 (so nil would be grouped with the
higher numbers), the comparison operator is not useful without further
logic-- but not when checking for people who don't have pets over 6 (nil
grouped with the lower numbers).

In an another example, it's possible when ordering a list, all nil examples
should be considered greater than, so that for example in an alphabetized
list nil values should appear at the end of the list.

It's intuitive that nil is less than everything, but in plenty of scenarios
that isn't useful.

>         * Is the problem being addressed significant enough to warrant a
> change to Swift?

Yes. Often where these are currently used, as in the proposal examples, the
results can be surprising and not useful.

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

More information about the swift-evolution mailing list