[swift-users] Weird function call behaviour

David Hart david.w.hart at me.com
Wed Feb 22 11:39:19 CST 2017


In the following piece of code, can somebody explain the last result? Why the break in consistency? Is this a bug?

protocol P {
	func foo() -> String
}

extension P {
	func foo() -> String { return "P" }
}

class A : P {
	func foo() -> String { return "A" }
}

class B : P {}
class C : B {
	func foo() -> String { return "C" }
}

A().foo()          // A
(A() as P).foo()   // A
B().foo()          // P
(B() as P).foo()   // P
C().foo()          // C
(C() as P).foo()   // P
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170222/7130b28d/attachment.html>


More information about the swift-users mailing list