I would like to specify a default implementation for an initializer in a protocol, so that I can run code on all structs conforming to a specific protocol.
I have the following example:
protocol ProtWithInit {
init()
}
extension ProtWithInit {
init() {
self.init()
print("Hello protocol")
}
}
struct MyStruct {}
extension MyStruct: ProtWithInit {}
let myStruct = MyStruct()
What I would like the MyStruct() call to do, is output "Hello protocol". However, nothing is being outputted when I run the above code.
Additionally, I would like to add to the initializer in the struct. So for example in the struct I add print("Hello struct") to the initializer and this would then outut both hello protocol and hello struct.
Is there any way I can achieve this?