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

Károly Lőrentey karoly at lorentey.hu
Mon Jul 18 09:40:09 CDT 2016


> On 2016-07-12, at 20:26, Chris Lattner via swift-evolution <swift-evolution at swift.org> wrote:
> The review of "SE-0121: Remove `Optional` Comparison Operators" begins now and runs through July 19. The proposal is available here:
> 
> 	https://github.com/apple/swift-evolution/blob/master/proposals/0121-remove-optional-comparison-operators.md <https://github.com/apple/swift-evolution/blob/master/proposals/0121-remove-optional-comparison-operators.md>

> 	* What is your evaluation of the proposal?

If SE-0123 is accepted, then I don’t mind keeping optional comparisons. But I also wouldn’t protest much against removing them until Optional can be made to conditionally conform to Comparable.

If SE-0123 is rejected, then definitely +1; I’ve been bitten several times by accidentally using these while handling the return value of Collection’s index(of:).

I don’t remember ever intentionally using these overloads, except ironically:

https://twitter.com/lorentey/status/657254631660236800 <https://twitter.com/lorentey/status/657254631660236800>

Note though that I often find myself wishing for Optional to implement Comparable. Writing comparison methods for little one-off Comparable structs that wrap Optionals gets tiring after a while. (As the proposal states, the existing overloads fall far short of achieving conditional conformance. I guess I could use these operators in the implementation of my wrapper structs, but I forget they exist.)

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

This depends on the outcome of SE-0123, which aims to eliminate the pitfall that makes these overloads dangerous. 

> 	* Does this proposal fit well with the feel and direction of Swift?

I think Optional should implement Comparable whenever the wrapped type does. The proposal is a distinct step back from this. On the other hand, if SE-0123 fails, I think it’s worth giving up on this goal in favor of removing a common pitfall.

> 	* If you have used other languages or libraries with a similar feature, how do you feel that this proposal compares to those?

Languages with implicit optionals do allow comparisons. E.g., "NSNotFound < 42" produces no compiler diagnostic. 

C++ has recently gained std::optional, which does provide </<=/>/>= operators, with the same semantics as Swift, including support for comparing optionals with non-optionals. It also has implicit promotion of values to optionals.
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3672.html#rationale.relops <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3672.html#rationale.relops>

Rust’s std::Option<T> enum implements the std::cmp::Ord trait when T does. So Rust’s optionals are (conditionally) comparable. I believe Rust provides no implicit coercion from T to std::Option<T>, but to be honest I’m not entirely sure.
https://doc.rust-lang.org/std/option/enum.Option.html#method.cmp <https://doc.rust-lang.org/std/option/enum.Option.html#method.cmp>

So the few languages I know that support explicit optionals also make them comparable.

> 	* How much effort did you put into your review? A glance, a quick reading, or an in-depth study?

Quick reading and minimal research.

-- 
Karoly
@lorentey

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


More information about the swift-evolution mailing list