Method dispatch documetation

Where can I read about method dispatch? Any documentation, proofs, source code, etc.
The most common information, as I know, is this research https://www.raizlabs.com/dev/2016/12/swift-method-dispatch/

But what is the official description? I mean both Swift and iOS.

1 Like

Swift is very similar to other single-dispatch languages such as C++ where there's a virtual method table. There's mention of them in the ABI stability manifesto, but it doesn't go into detail. There are also things called protocol witness tables, which are like virtual method tables, but are for when a protocol is being used as an existential. They're also mentioned in the manifesto, without implementation details.

But as that linked article states, there are some edge cases in dispatching. Such requiring @objc on methods defined in extensions when you want to override that method in a child class. And how extension methods are statically dispatched.

Actually, following the ticket mentioned for v-tables, I've found a nice write-up by @Andrew_Trick of their potential implementation. I'm not sure if that is still valid, but it gives some insight.

That writeup was specific to dispatching across stable binary boundaries as part of defining the ABI. In it I assumed that methods could be associated with a stable integer index. If my memory is correct, someone determined that it wasn't feasible to do that across library revisions. i.e. My proposal is incompatible with library evolution. @Slava_Pestov came up with an alternative design for purely symbolic dispatch across resilient library boundaries using exported symbols. But dispatch within a library, or across non-resilient modules can be implemented with v-table or witness table dispatch.

I do feel strongly that all ABI decisions should be specified in docs/ABI. It's hard to sort out the implementation from the ABI by reading the source. We're not there yet.

However, hopefully most calls don't need to go through the ABI's dispatch mechanism. Since that can change with each version of the compiler, documentation will never be authoritative. It's only as good as its timestamp. The blog post above does seem well written at first glance...

2 Likes