Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Swift have dynamic dispatch and virtual methods?

Coming form a C++/Java/C# background I was expecting to see virtual methods in Swift, however reading the swift documentation I see no mention of virtual methods.

What am I missing?


Due to large number of views, I have decided to offer a reward for an upto date and very clear/detail answer.

like image 985
Ian Ringrose Avatar asked Jun 03 '14 11:06

Ian Ringrose


People also ask

What type of method dispatch is used in Swift?

V-Table Dispatch. This is the default dispatch method used in Swift for reference types.

What is dynamic dispatch in IOS Swift?

Dynamic dispatch. It simply means that the Objective-C runtime decides at runtime which implementation of a particular method or function it needs to invoke.

What is virtual function in Swift?

In C++, a virtual function is a member function which is declared within a base class and is overriden by a derived class.

What is the difference between static dispatch and dynamic dispatch Swift?

The two most well-known dispatch types we have in Swift are the Static and the Dynamic one. With Static dispatch, the program determines which method implementation should be executed at compile time; on the other hand, with Dynamic dispatch, the program determines it at runtime.


2 Answers

Unlike C++, it is not necessary to designate that a method is virtual in Swift. The compiler will work out which of the following to use:

(the performance metrics of course depend on hardware)

  • Inline the method : 0 ns
  • Static dispatch: < 1.1ns
  • Virtual dispatch 1.1ns (like Java, C# or C++ when designated).
  • Dynamic Dispatch 4.9ns (like Objective-C).

Objective-C of course always uses the latter. The 4.9ns overhead is not usually a problem as this would represent a small fraction of the overall method execution time. However, where necessary developers could seamlessly fall-back to C or C++. In Swift, however the compiler will analyze which of the fastest can be used and try to decide on your behalf, favoring inline, static and virtual but retaining messaging for Objective-C interoperability. Its possible to mark a method with dynamic to encourage messaging.

One side-effect of this, is that some of the powerful features afforded by dynamic dispatch may not be available, where as this could previously have been assumed to be the case for any Objective-C method. Dynamic dispatch is used for method interception, which is in turn used by:

  • Cocoa-style property observers.
  • CoreData model object instrumentation.
  • Aspect Oriented Programming

The kinds of features above are those afforded by a late binding language. Note that while Java uses vtable dispatch for method invocation, its still considered a late binding language, and therefore capable of the above features by virtue of having a virtual machine and class loader system, which is another approach to providing run-time instrumentation. "Pure" Swift (without Objective-C interop) is like C++ in that being a direct-to-executable compiled language with static dispatch, then these dynamic features are not possible at runtime. In the tradition of ARC, we might see more of these kinds of features moving to compile time, which gives an edge with regards to "performance per watt" - an important consideration in mobile computing.

like image 113
Jasper Blues Avatar answered Nov 16 '22 00:11

Jasper Blues


All methods are virtual; however you need to declare that you are overriding a method from a base class using the override keyword:

From the Swift Programming Guide:

Overriding

A subclass can provide its own custom implementation of an instance method, class method, instance property, or subscript that it would otherwise inherit from a superclass. This is known as overriding.

To override a characteristic that would otherwise be inherited, you prefix your overriding definition with the override keyword. Doing so clarifies that you intend to provide an override and have not provided a matching definition by mistake. Overriding by accident can cause unexpected behavior, and any overrides without the override keyword are diagnosed as an error when your code is compiled.

The override keyword also prompts the Swift compiler to check that your overriding class’s superclass (or one of its parents) has a declaration that matches the one you provided for the override. This check ensures that your overriding definition is correct.

like image 34
trojanfoe Avatar answered Nov 16 '22 01:11

trojanfoe