[swift-evolution] [Draft][Proposal] Formalized Ordering

Pyry Jahkola pyry.jahkola at iki.fi
Sun Jul 24 23:06:35 CDT 2016


> Dave Abrahams wrote:
> 
> Asking whether zero appears in a sequence that happened to contain a NaN would equally be a precondition violation.

No, it wouldn't because Double would implement Equatable + Comparable by providing custom implementations of ==, <, >, <=, and >=, in addition to Double.<=> which may trap.

My point in https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160718/025375.html is that by keeping all those operators within the required interface of protocol Comparable, such customisation remains possible. The original proposal only suggested == in Equatable and <=> in Comparable. If that were the case, then generic algorithms would necessarily end up calling <=>.

It is key that not only <=> is a customisation point of Comparable. Only those generic algorithms which used <=> directly (e.g. sort, partition, binary search) would see precondition violations if the sequence contained one or more NaNs.

***

Another possible choice would be to return .descending whenever either of the comparands were NaN, while also making <, >, <=, and >= return false in such cases. Then we wouldn't see preconditionFailures but instead produced bogus results from sort, partition etc. That's the tradeoff Haskell has chosen for its `compare` function over Double, FWIW.

— Pyry
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160725/1d207c55/attachment.html>


More information about the swift-evolution mailing list