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

Kevin Ballard kevin at sb.org
Fri Dec 11 19:12:00 CST 2015

On Thu, Dec 10, 2015, at 09:27 PM, Paul Cantrell wrote:
>> Method invocation syntax does not inherently mean "dynamic dispatch"
>> any more than function syntax does.
> In languages now in widespread use — Java, Ruby, Javascript, C#, what
> I know of Python — that is _precisely_ what it means. In all those
> languages, as far as I know:
>  * *all* uses of the dot invocation syntax either use dynamic
>    dispatch, or have something on the left whose compile-time and
>    runtime types are identical; and
>  * *all* instances of dynamic dispatch in the language either use the
>    dot invocation syntax, or have and implicit “self.” / “this.”  that
>    uses it.
> In short, programmers are likely to bring to Swift the assumption that
> dot invocation = dynamic dispatch.

I know almost nothing about C#, so let's ignore that for a moment.

The other languages you listed don't have some notion that dot-syntax
means dynamic dispatch, instead they're just dynamically-dispatched
languages in general. Java, Ruby, and Python are object-oriented
languages, so all interactions with objects are always dynamic.
JavaScript is a prototype-based language, but the same basic idea
applies. Python has a bunch of global functions that are actually
dynamically dispatched based on the argument runtime type (e.g.
`len(foo)` is actually `foo.__len__()`). JavaScript, Python, and Ruby
are all scripting languages so they don't even know at compile-time what
a function invocation resolves to, all function calls are resolved using
the same scoping rules that affect variables, and in Python and
JavaScript (not sure about Ruby) these functions are actually defined on
an object that's automatically in the scope, e.g. in Python the `len()`
function is actually `__builtins__.len()`, and you can say
`__builtins__.len = None` if you want to break any code that tries to
call the function.

So really there's just two classes of language that you've called out
that are predominately dynamic dispatch: Pure-OOP languages (like Java)
and scripting languages. And that really shouldn't be a surprise that
they're dynamic dispatch. But that's not inherent in the syntax.

Meanwhile, there's languages that use dot-notation for static dispatch.
The first language on my list is Swift, because that's absolutely true
for structs and enums. It's even somewhat true for generics; I say
"somewhat" because the behavior for generics is identical whether it's
virtual dispatch or static dispatch, and in fact it uses virtual
dispatch for the basic version of the function and uses static dispatch
for all specializations, so it's just as valid to say that dispatch on
generics is static as it is to say that it's virtual.

Beyond that, Rust uses static dispatch (with optional virtual dispatch
using trait objects, but most uses of traits—i.e. in generics—is still
static dispatch). C++ uses static dispatch by default and requires the
`virtual` keyword to enable virtual dispatch. I'm sure there's other
examples too, but I don't have a comprehensive knowledge of all
programming languages that use dot-syntax.

We're also only considering single-dispatch at the moment, where the
method is dispatched based on the receiver type. There's languages that
are multiple-dispatch, and even Swift supports this using
function/method overloading. I don't know enough about multiple-dispatch
languages like Dylan and Julia to know if they use static or dynamic
dispatch though.

Overall, my point is that when you say that "programmers are likely to
bring to Swift the assumption that dot invocation = dynamic dispatch",
you're speaking from a very limited point of view. People who only have
experience with Java, Ruby, Python, and JavaScript might think this,
although they also might think that all functions are resolved
dynamically at runtime, or that everything is an object, and those are
certainly not true. People who come from a different background, such as
C++, or Rust, may think that dot invocation == static by default. Or
people might come from a background that doesn't have dot invocation at
all, or maybe they'll come from a mixed background and recognize that
there's a wide variety of dispatching and method resolution strategies
and that each language does things its own way.

-Kevin Ballard
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151211/89b63cec/attachment.html>

More information about the swift-evolution mailing list