[swift-users] [swift-user]Unexpected behavior of protocol extension.

Игорь Никитин devnikor at icloud.com
Tue Sep 20 01:04:11 CDT 2016


Looks like a bug according to dispatch rules (here <https://medium.com/ios-os-x-development/swift-protocol-extension-method-dispatch-6a6bf270ba94#.8zlhlu8s6> and here <https://nomothetis.svbtle.com/the-ghost-of-swift-bugs-future?utm_campaign=iOS+Dev+Weekly&utm_medium=web&utm_source=iOS_Dev_Weekly_Issue_203>. Also similar question on SO <http://stackoverflow.com/questions/34847318/swift-protocol-extension-method-dispatch-with-superclass-and-subclass>)
It should call dynamic bar implementation

> 20 сент. 2016 г., в 5:11, Zhao Xin via swift-users <swift-users at swift.org> написал(а):
> 
> See below code.
> 
> protocol Foo {
>     func bar()
> }
> 
> extension Foo {
>     func bar() {
>         print("I am bar.")
>     }
> }
> 
> class A:Foo {
>     func output() {
>         print(type(of:self)) // prints "B".
>         self.bar() // prints "I am bar."
>         (self as! B).bar() // prints "I am B."
>     }
> }
> 
> class B:A {
>     func bar() {
>         print("I am B.")
>     }
> }
> 
> let b = B()
> b.output()
> 
> I thought `self.bar()` would do the same as `(self as! B).bar()`. It didn't. In my opinion,  `type(of:self) is B.type`, so they should be the same, shouldn't they?
> 
> Zhaoxin
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160920/b293c91d/attachment.html>


More information about the swift-users mailing list