|
|
|
|
|
by Buckleyisms
4382 days ago
|
|
From the linked Wikipedia article on Dynamic Dispatch: > C++ compilers typically implement dynamic dispatch with a data structure called a virtual table that defines the message to method mapping for a given class (C++ as such has no notion of a vtable). In my article, I'm specifically talking about message passing as implemented in Objective-C and Smalltalk. http://en.wikipedia.org/wiki/Message_passing Both the term "dynamic dispatch" and the term "message passing" are overloaded, but I used the term "message passing" to differentiate it from vtable dispatch, which I talk about in the article. I'm not criticizing Swift for being a statically-typed language. I'm only criticizing the way it calls methods. |
|
* substitute object type without recreating the object
* substitute object's method without changing object's type
* create a string with method name and call that method
Obj-C and most dynamic languages have them; Swift and languages like C++ do not. (I understand that with full reflection and enough runtime hacking static languages can have that too — by the way, that includes Swift as it currently is; but that's a whole different discussion).
In other words: if the compiler has statically (at compile time) validated types of arguments and return values of any `obj.foo(arg)` call, why would anyone possibly want anything but either a direct call to a hardcoded address, or to a pointer stored at vtable? And if the compiler has no means to validate that, that means you are down to what dynamic languages do: there has to be some code to check argument types before passing them to the code that makes hard assumptions about their types.