[swift-evolution] Proposal: Universal dynamic dispatch for method calls

Paul Cantrell cantrell at pobox.com
Thu Dec 10 08:08:24 CST 2015


>> I don't agree that Swift's general slant is towards using virtual dispatch. Only classes and protocol objects use virtual dispatch[1]. And Swift even provides a mechanism (final / static) to remove dynamic dispatch from classes. It seems to me that Swift's general slant is towards encouraging static dispatch whenever possible.
> 
> I quite disagree here.
> 
> Swift loves to dispatch things statically and does so wherever possible. In some cases—such as value types not having inheritance—language features are clearly designed the way they are specifically to allow them to be statically dispatched. But Swift never uses static dispatch where dynamic dispatch would have given a different result.

Exactly so.

Also note that, as I laid out in my original post, this is _especially_ true of the `foo.bar()` syntax — which in most programming languages is the syntax specifically set aside for dynamic dispatch. I’d prefer that the semantics of a particular syntax be as uniform as possible.

I’m a lot less surprised about a function overload, for example, being resolved by the static type of its arguments than I am about `foo.bar()` depending on the static type of foo.

If it’s not just Kevin who disagrees with the assertion that static dispatch of `foo.bar()` is unexpected and a likely source of programmer error, then we should stop arguing about it and gather some data.

Cheers, P



More information about the swift-evolution mailing list