I have the following:
protocol Guard {
var prisoners: Array<Prisoner>? { get set }
func smack<T: Prisoner>(prisoner: T)
func smackAll()
}
extension Guard {
final func smackAll() {
prisoners?.forEach() { smack($0) }
//ERROR: "Cannot invoke 'smack' with an argument list of type '(Prisoner)'"
}
}
protocol Prisoner {
var guards: Array<Guard>? { get set }
}
extension Prisoner {
final func incurBeating() {
guards?.forEach() { $0.smack(self) }
}
}
I wish to assign the Guard
protocol to any number of objects of different class types, and Prisoner
to other objects. The objects acting as guards will have their own smack implementations, according to their class type. But I see no reason to re-write smackAll
in each class that might inherit Guard
. So I'm trying to do it like this, but the compiler isn't letting me. What am I missing?