[swift-dev] Should we remove _customContainsEquatableElement from stdlib?

Kevin Ballard kevin at sb.org
Wed Dec 30 18:50:30 CST 2015

FWIW, once we have conditional protocol conformance (`extension ... : Proto where ...`), and if we get rid of the circular protocol inheritance limitation (two protocols that inherit from each other, which should in theory be fine), then we can say something like

protocol EquatableSequenceType : SequenceType {
    func contains(element: Self.Generator.Element) -> Bool

extension SequenceType : EquatableSequenceType where Self.Generator.Element : Equatable {
    func contains(element: Self.Generator.Element) -> Bool {
        // ...

This way the method can actually be overridden directly without requiring any hacks like _customContainsEquatableElement.

We could work around the circular protocol inheritance thing by declaring a `typealias _Element : Equatable` in EquatableSequenceType instead of having the inheritance, and then just set `typealias _Element = Self.Generator.Element` in the protocol extension on SequenceType, but that does in theory let implementing types actually change the type of the contains() method parameter by overriding the typealias, which is a bit weird. Alternatively, we could work around it by allowing you to say `extension Any : EquatableSequenceType where Self : SequenceType { ... }` and having that essentially extend every concrete type that conforms to SequenceType, which means SequenceType itself doesn't conform to EquatableSequenceType and therefore there's no circular protocol inheritance, but I'm not sure if this is actually an approach we want to take (although this is precisely what Rust does and it works for them).

-Kevin Ballard

On Wed, Dec 30, 2015, at 10:34 AM, Ling Wang via swift-dev wrote:
> After reviewing the code of stdlib I found no one actually implements _customContainsEquatableElement:
> 1. Its default implementation in `SequenceType` just returns nil.
> 2. The implementation in `Set` delegates to `contains` which is bad because it reverses their relationship: the default implementation of `contains` in `SequenceType` delegates to `_customContainsEquatableElement`.
> 3. In all other place it just delegates to another `SequenceType`.
> So no one is doing real work.
> If the current _customContainsEquatableElement is only a relic I suggest we remove it and clean up related code.
> _______________________________________________
> swift-dev mailing list
> swift-dev at swift.org
> https://lists.swift.org/mailman/listinfo/swift-dev

More information about the swift-dev mailing list