protocol Container {
associatedtype Item: Equatable
mutating func append(_ item: Item )
var count: Int { get }
subscript(i: Int) -> Item { get }
}
extension Container where Item == Double {
func average() -> Double {
var sum = 0.0
for index in 0..<count {
sum += self[index]
}
return sum / Double(count)
}
}
print([1260.0, 1200.0, 98.6, 37.0].average()) // Prints "648.9"
What does the bold part mean?
Does it mean "add this average() method requirement to the Container protocol whose associated type Item is of type Double" ?
or does it mean "add the average() method to the conforming types of Container whose Item is of type Double" ?
Is this extension adding a method requirement to the Container protocol itself, or is it adding this method to the types who conform to Container ?
Also, the last line of code uses the average() method on an array of Double values. But how is that even possible? does this array even conform to the Container protocol??