Within exercise on generic composition it it stated that: "a return type of Car means that addCAI() and func addTBE() could return any kind of Car. Different calls to the function could return instances of different Car-conforming types. That means that a hypothetical variant addTBE() function could decide whether to add cold air intake twice. This sample implementation would either return a ColdAirIntake or a ColdAirIntake<ColdAirIntake>, based on a Bool.random(). This is a flexible approach, but it may or may not be what you want. Right now, a caller of this function must account for the possibility that addTBE() might return items with different options from one call to the next". However when run the function addTBE() returns all the time the same rusult. Versus code stated in book, there was error, so I changed part of one line as commented out below, however still the same output. If you can advice how to get different output from function using if Bool.random() within generic struct.
protocol Car {
var options: String {get}
}
struct Azda: Car {
var kind = "8 MPS"
var options: String {
"Azda \(kind)"
}
}
func options<T: Car>(_ car: T) {
print("Love to ride \(car.options)")
}
options(Azda())
struct Tuner {
private struct ColdAirIntake<Tuning: Car>: Car {
var car: Tuning
var options: String {
"\(car.options) air cooled"
}
}
private struct TurboBackExhaust<Tuning: Car>: Car {
var car: Tuning
var options: String {
"\(car.options) with turbo back exhaust"
}
}
func addCAI() -> some Car {
return ColdAirIntake(car: Azda())
}
func addTBE() -> some Car {
var coolIn = ColdAirIntake(car: Azda())
if Bool.random() {
coolIn = ColdAirIntake(car: Azda()) // coolIn = ColdAirIntake(car: coolIn)
}
return TurboBackExhaust(car: coolIn)
}
}
var tuner = Tuner()
var frontBack = tuner.addTBE()
options(frontBack)
var front = tuner.addCAI()
options(front)