[swift-users] [swift-user]Unexpected behavior of protocol extension.
Zhao Xin
owenzx at gmail.com
Mon Sep 19 21:11:50 CDT 2016
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160920/8a6a40d8/attachment.html>
More information about the swift-users
mailing list