Comparable and FloatingPoint types

Completely agree with this point.

FWIW, I consider #1 to be the default option that we should go with unless there is something clearly better. We have had this behavior for years now and (as you say) it is highly precedented in other languages. I agree it is suboptimal, but not enough to be worth heroic solutions given where we are.

I haven't carefully considered this, but what about a variant of this:

  • Do not change any existing behavior.
  • Introduce an new/additive <=> operator, returning less/greater/equal/unordered, which is (by default) implemented in terms of the < and == operators for a type. Floating point types would override this default implementation with the obvious impl.

This solution is nice because:

  1. It does not affect source or ABI compatibility.

  2. sort and specific other algorithms could (optionally) be changed to use this, incrementally fixing a bug for FP types and possibly making those algorithms more efficient. If done in time for Swift 5, this operator could might even be able to be added as a requirement for Comparable types.

  3. It is additive, so it can be done at any time.

  4. For users that hit surprising behavior in practice, e.g. your contains example, they can use this (along the a closure predicate) to get the behavior they want.

Have you considered an approach like this?

-Chris

5 Likes