Is it possible to provide an extension that adds only functionality to classes that conform to the protocol? The functionality I'm trying to achieve is something like this:
protocol Identifiable {
var id: String { get }
}
class Model {
func report(data: String) {
...
}
}
class Thing: Model, Identifiable {
var id: String
...
}
class Place: Model, Identifiable {
var id: String
...
}
extension (Model + Identifiable) {
func identifiy() {
report("\(self.id)")
}
}
Place().identify()
Thing().identify()
Extension of the protocol itself is not possible, since expansion requires access to the methods defined on model. Extension model crashes because it idis defined only for child objects. Extension is Model: Identifiablenot performed because it modeldoes not comply with the protocol Identifiable.
source
share