[swift-evolution] [Proposal] Explicit Synthetic Behaviour

Xiaodi Wu xiaodi.wu at gmail.com
Sat Sep 9 11:48:20 CDT 2017


On Sat, Sep 9, 2017 at 07:51 Brent Royal-Gordon <brent at architechies.com>
wrote:

> On Sep 8, 2017, at 6:03 PM, Xiaodi Wu via swift-evolution <
> swift-evolution at swift.org> wrote:
>
> For any open protocol (i.e., a protocol for which the universe of possible
> conforming types cannot be enumerated a priori by the protocol designer)
> worthy of being a protocol by the Swift standard ("what useful thing can
> you do with such a protocol that you could not without?"), any sufficiently
> interesting requirement (i.e., one for which user ergonomics would
> measurably benefit from a default implementation) either cannot have a
> universally guaranteed correct implementation or has an implementation
> which is also going to be the most performant one (which can therefore be a
> non-overridable protocol extension method rather than an overridable
> protocol requirement with a default implementation).
>
>
> Counter-example: `index(of:)`, or rather, the underscored requirement
> underlying `index(of:)`. The "loop over all indices and return the first
> whose element matches" default implementation is universally guaranteed to
> be correct, but a collection like `Set` or `SortedArray` can provide an
> implementation which is more performant than the default.
>

Don't get me started on Swift's handling of equality and arrays with NaN.
_customIndexOfEquatable, if I'm not mistaken, is a part of that whole
tangle of performance optimizations which gleefully refuse to acknowledge
Equatable's semantic peephole for some values of a type being unordered
with respect to everything else. In a world where this trade-off between
performance and correctness had not been taken, I don't imagine that it
would be possible to make the protocol extension method 'index(of:)' any
more performant than 'index(where: { $0 == $1 })'.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170909/8c996ee0/attachment.html>


More information about the swift-evolution mailing list