I write a custom storage, that should supports only objects that conform some protocol:
protocol MyBaseProtocol {
func baseMethod()
}
class Stor <ElementType : MyBaseProtocol> {
var storage = [ElementType]()
func addElement(_ element: ElementType) {
storage.append(element)
}
}
Next I've created a child protocol and want to store only objects what conform the child protocol:
protocol MyProtocol : MyBaseProtocol {
func method()
}
var otherStorage = Stor<MyProtocol>() //compilation error
class C1 : MyProtocol {
func method() {
}
func baseMethod() {
}
}
class S1 : MyProtocol {
func method() {
}
func baseMethod() {
}
}
otherStorage.addElement(C1())
otherStorage.addElement(S1())
I've got an error:
Value of protocol type 'MyProtocol' cannot conform to 'MyBaseProtocol'; only struct/enum/class types can conform to protocols
How I can create an instance of Stor that can store only objects that conform MyBaseProtocol?