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)
CodePudding user response:
The reason is the lines where you say:
if Bool.random() {
coolIn = ColdAirIntake(car: Azda()) // coolIn = ColdAirIntake(car: coolIn)
}
The second line where you set coolIn has no effect on the underlying options property of the Azda struct you passed in. I was able to rectify the problem by using the random boolean in the constructor of Azda:
struct Azda: Car {
var kind = "8 MPS"
var options: String {
if(Bool.random()){
return "Azda Option A"
}
else{
return "Azda Option B"
}
}
}
Which outputs a random result each time. There are many solutions to this problem but the bottom line is that fundamentally your call to Boolean.random() does not do anything because it does not alter the underlying default value of the Azda instance you are using.
