> On Feb 5, 2017, at 7:45 AM, Karl Wagner <razie...@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
_______________________________________________
swift-dev mailing list
swift-dev@swift.org
https://lists.swift.org/mailman/listinfo/swift-dev

Reply via email to