December 2015
Intermediate to advanced
400 pages
13h 3m
English
A natural question to ask about an instance of ExerciseType is how many calories were burned per minute of exercise.
You can use your knowledge of generics and where clauses to write a function that will perform that calculation.
Listing 23.4 Computing calories burned per minute, generically
...
func caloriesBurnedPerMinute<Exercise: ExerciseType>(exercise: Exercise) -> Double {
return exercise.caloriesBurned / exercise.minutes
}
print(caloriesBurnedPerMinute(ellipticalWorkout))
print(caloriesBurnedPerMinute(runningWorkout))
caloriesBurnedPerMinute(_:) is a generic function whose placeholder type is required to be a type that conforms to the ExerciseType protocol. The body of the function uses two of ...
Read now
Unlock full access