[swift-dev] Resilient dynamic dispatch ABI. Notes and mini-proposal.

Andrew Trick atrick at apple.com
Mon Feb 6 12:31:49 CST 2017


> On Feb 5, 2017, at 7:45 AM, Karl Wagner <razielim at gmail.com> wrote:
> 
> I have a question about current dispatching behaviour with protocols and ‘Self’.
> 
> protocol CustomEquatable {
>     func equal(to: Self) -> Bool
> }
> 
> open class Super : CustomEquatable {
>     func equal(to: Super) -> Bool { print("super"); return false }
> }
> class Sub: Super {
>     func equal(to: Sub) -> Bool { print("sub-sub"); return true }
>     override func equal(to: Super) -> Bool { print("sub-super"); return true }
> }
> 
> Sub().equal(to: Sub())     // sub-sub
> Sub().equal(to: Super())   // sub-super 
> Super().equal(to: Sub())   // super
> Super().equal(to: Super()) // super
> 
> (Sub() as Super).equal(to: Sub)              // sub-super — dynamically dispatched to callee’s type, not param
> (Sub() as Super).equal(to: (Sub() as Super)) // sub-super — as above
> 
> 
> Currently, we dynamically dispatch to the callee’s type to find ‘Self’, but we don’t apply that consistently when dispatching to ‘Self’-type parameters. Is that expected behaviour?
> 
> - Karl

Hi Karl,

This is expected behavior. The choice of method overload is based on the static types: `Super.equal(to: Super)`. The dynamic dispatch that you get when using the override keyword is based on the `self` argument’s dynamic type only. The dynamic types of the non-self arguments don’t play a part.

I’m sure there are some good language design reasons not to support dynamic method overloads. I can only tell you that implementing it would be no fun.

-Andy
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-dev/attachments/20170206/42dc83e0/attachment.html>


More information about the swift-dev mailing list