I\'m doing something similar to the Bridge Pattern in JAVA, DriverType
is a protocol requires a property named vehicle
to be a Drivable
I think the compiler error is misleading. DriverType
states that any class adopting it must expose a vehicle
property with Drivable
type, not a property with a class type adopting the Drivable
type.
I would solve this issue by defining both the DriverType
protocol and the Car
class using generics:
protocol Drivable {
var speed: Double { get }
init()
}
protocol DriverType {
typealias T: Drivable
var vehicle: T { get }
}
class Car: Drivable {
var speed = 80.0;
var brand = "BMW"
required init() {}
}
class Driver<T: Drivable>: DriverType {
var vehicle: T = T()
}
This explicitly states that classes adopting DriverType
must expose a vehicle
property whose type is any class adopting the Drivable
protocol.