Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift language statically or dynamically dispatched?

Tags:

swift

Reading Swift language guide I cannot find explicit information whether Swift is statically dispatched (like basic C++, Java, C#) or dynamically dispatched (like Objective-C).

The documentation of language features (like classes, extensions, generics, etc) seems to suggest that it is statically typed, which might be the source of supposed speed improvements. However, Apple stated on the WWDC 2014 keynote that the language uses the same runtime as Objective-C, and is very compatible with Cocoa/Cocoa Touch, which suggest dynamic dispatch.

like image 407
Maciej Jastrzebski Avatar asked Jun 03 '14 06:06

Maciej Jastrzebski


2 Answers

Describing C++, Java, and C# as statically dispatched is not particularly accurate. All three languages can and often do use dynamic dispatch.

Swift, similarly, can do both. It does differ from ObjC in that it does not always dynamically dispatch. Methods marked @final can be statically dispatched, as can struct and enum methods. I believe non-final methods can be statically dispatched if the runtime type can be proven at compile time (similar to C++ devirtualization), but I'm not certain about the details there.

like image 79
Catfish_Man Avatar answered Sep 30 '22 18:09

Catfish_Man


According to this excerpt from Y Combinator (https://news.ycombinator.com/item?id=7835099):

From a user's point of view, it's basically straight out of the Rust book, all the gravy with also relaxed ownership and syntax.

It has it all [1]: static typing, type inference, explicit mutability, closures, pattern matching, optionals (with own syntax! also "any"), generics, interfaces, weak ownership, tuples, plus other nifty things like shorthand syntax, final and explicit override...

It screams "modern!", has all the latest circlejerk features. It even comes with a light-table/bret-victor style playground. But is still a practical language which looks approachable and straightforward.

Edit: [1]: well, almost. I don't think I've caught anything about generators, first-class concurrency and parallelism, or tail-call optimization, among others.

like image 31
Tash Pemhiwa Avatar answered Sep 30 '22 18:09

Tash Pemhiwa