Inheritance of a primary associated type on a derived protocol?

Hello,

While re-reading SE-0346 Lightweight same-type requirements for primary associated types, I could not decide for good whether the code below is the way to inherit a primary associated type from a base protocol:

protocol Base<Element> {
  associatedtype Element
}

protocol Derived<Element>: Base { }

The proposal does not explicitly mention this scenario.

It happens to work with Swift 5.10, which compiles this code (as expected):

protocol Base<Element> {
  associatedtype Element
}

protocol Derived<Element>: Base { }

func f(_ x: some Derived<Int>) { }

And Swift 5.10 rejects the following variants (as expected as well):

protocol Base<Element> {
  associatedtype Element
}

protocol Derived: Base { }

// Protocol 'Derived' does not have primary associated
// types that can be constrained
func f(_ x: some Derived <Int>) { }
protocol Base<Element> {
  associatedtype Element
}

// An associated type named 'AnotherName' must be
// declared in the protocol 'Derived'
// or a protocol it inherits.
protocol Derived<AnotherName>: Base { }

The diagnostic in the last sample code is a strong hint that I'm on the right track.
But I'm just looking for the confirmation that when I write protocol Derived<Element>: Base { }, I'm not relying on a compiler glitch that was not intended.

Apologies for the useless question. The standard Collection is declared as:

public protocol Collection<Element> : Sequence { ... }

I don't need further confirmation :sweat_smile:

2 Likes