Can I make an enum
generic (different type for each case), so I can use its cases to infer a type for a generic class?
I have an example here:
class Config {
let name: String
init(_ name: String) {
self.name = name
}
}
class CarConfig: Config {
static let sports = CarConfig("sports")
static let van = CarConfig("van")
}
class BikeConfig: Config {
static let road = BikeConfig("road")
static let mtb = BikeConfig("mtb")
}
enum VehicleType {
case car, bike, scooter
}
class Vehicle<C: Config> {
let type: VehicleType
let config: C
init(type: VehicleType, config: C) {
self.type = type
self.config = config
}
}
let bike = Vehicle(type: .bike, config: BikeConfig.mtb)
bike.config.name // mtb
What I would like to do is initiate a Vehicle like this:
let bike = Vehicle(type: .bike, config: .mtb)
I want the compiler to infer the BikeConfig
, so I can leave it out. I want the compiler to know that a Vehicle
with type == VehicleType.bike
always has a Config
that is a BikeConfig
.
I would have to change my Vehicle
obviously:
class Vehicle<C: Config> {
let type: VehicleType<C>
let config: C
init(type: VehicleType<C>, config: C) {
self.type = type
self.config = config
}
}
And now make enum VehicleType
enum VehicleType<C: Config>
.
No Idea where to go from here though. Any help? :)
[UPDATE: added scooter
case to VehicleType
]