I'm struggling to implement this protocol using another protocol as its associatedtype.
I have these two:
public protocol CarToken: Any {
}
public protocol Car: AnyObject {
associatedtype Token: CarToken
func doThis(_ token: Token)
func doThat(_ token: Token)
}
If I try to implement the Car like this, it works:
class ObjectThatImplementsCarToken: CarToken {
}
extension FooBar: Car {
func doThis(_ token: ObjectThatImplementsCarToken) { ... }
func doThat(_ token: ObjectThatImplementsCarToken) { ... }
}
But if instead I use a protocol ProtocolThatExtendsCarToken, the compiler won't accept:
protocol ProtocolThatExtendsCarToken: CarToken {
}
// Error:
extension FooBar: Car {
func doThis(_ token: ProtocolThatExtendsCarToken) { ... }
func doThat(_ token: ProtocolThatExtendsCarToken) { ... }
}
The compiler thinks FooBar doesn't conform to Car.
Why is that? I want those functions to accept multiple types that implements ProtocolThatExtendsCarToken, which by extension they all implement CarToken. Is that possible?