UPDATE:
My first question was wrong, please skip to the reasked question here.
OLD POST:
While working with SwiftNIO, I faced a problem, that was very hard to find out (for the background: I made a ChannelInboundHandler
subclass and implemented the channelRead
-func, but that function was never called because of a typo in my subclass).
But let's abstract the problem with dummy code:
protocol DummyProtocol {
func test(parameter1: String, parameter2: Int)
}
class DummyClass: DummyProtocol {
}
Of course I receive the Error: "Type 'DummyClass' does not conform to protocol 'DummyProtocol'"
Now let's say, that we implement the protocol correctly like this:
class DummyClass: DummyProtocol {
func test(parameter1: String, parameter2: Int) {
print("do sth. with \(parameter1) and \(parameter2+3)")
}
}
So the DummyClass
now has kind of a default implementation if someone wants to subclass it. So let's go further and make a subclass like this:
class DummySubClass: DummyClass {
}
Adding nothing to the class works fine, because the parent class implements the protocol.
The main problem starts, if you want to override these default implementations in the subclass, but if there is a mistake in the func name or func parameters like this:
class DummySubClass: DummyClass {
func test(parameter1Wrong: Float, parameter2Wrong: Bool) {
print("we don't get a warning, that class does not conform to the protocol")
}
}
I understand, that this is completely okay, because the code is not wrong. It's just implementing a new func with different parameters. But I was searching a long time in current project for the bug, because I had a typo in the parameter name and as the Framework (in this case SwiftNIO) will call the test
-method, the implemented code in the subclass never gets called, because it's a completely different method.
What do you think? In my opinion the compiler should check, if there are methods, that have the same name as those of the protocols and it should give a warning about that, that maybe the protocol isn't implemented correctly.