Let's say I have the following protocol:
protocol Driver {
associatedtype Car
}
And than a protocol inheriting from Driver
, explicitly setting Car
to be of type RaceCar
.
protocol RacingDriver: Driver {
typealias Car = RaceCar
}
Why do I still get the error
protocol 'RacingDriver' can only be used as a generic constraint because it has Self or associated type requirements
when doing var driver: RacingDriver
?
Background story:
Essentially what I would like to have is a variable which can be of any type conforming to Driver
, as long as that type has Car
defined to be a RaceCar
.
Example:
protocol Driver {
associatedtype Car
}
protocol RacingDriver: Driver {
typealias Car = RaceCar
}
struct NascarDriver: RacingDriver {}
struct IndyDriver: RacingDriver {}
struct NormalDriver: Driver {}
var racingDriver: RacingDriver // This could be either a NascarDriver or IndyDriver but not a NormalDriver!
How could I achieve this?
Is there a way around this other than type-erasure?
I would like to avoid subclassing from a RacingDriver
base class.
Thanks in advance!