Protocol extension method dispatch in Swift 2.0

喜夏-厌秋 提交于 2019-12-10 04:32:36

问题


I'm facing a problem regarding protocols methods dispatch.

I have a class hierarchy that looks like that:

protocol E {
    func test()
}

extension E {
    func test() {
        print("jello")
    }
}

class A: E {

}

class B: A {
    func test() {
        print("hello")
    }
}

But when I call test on an instance of class B statically forced to be typed A, "jello" gets printed, not "hello".

let b: A = B()  // prints "jello" not "hello"
b.test()

My understanding is that test method printing "jello" gets "integrated" into instances of A (since A conforms to E protocol). I'm then providing another implementation of test inside B (that inherits form A). I thought polymorphism would work here and calling test on B instance that are stored inside A references would print hello. What's happening here?

It's perfectly working when not using any protocol:

class A {
    func test() {
        print("jello")
    }
}

class B: A {
    override func test() {
        print("hello")
    }
}

let b: A = B() // prints "hello"
b.test() 

What's different from adopting a protocol that adds new methods to my parent class and providing a new implementation in a subclass, than having directly written this method in the parent class and then overriding it in a subclass?

Do you guys have any workaround?


回答1:


Smells like a bug.

The only workaround I came up with was very ugly...

protocol E {
    func test()
}

func E_test(_s: E) {
    print("jello")
}

extension E {
    func test() { E_test(self) }
}

class A: E {
    func test() { E_test(self) }
}

class B: A {
    override func test() {
        print("hello")
    }
}

let b: A = B()
b.test()



回答2:


It is a bug indeed. Here is the link to it: https://bugs.swift.org/browse/SR-103



来源:https://stackoverflow.com/questions/32734403/protocol-extension-method-dispatch-in-swift-2-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!