According to Apple :
When you mark a member declaration with the
dynamic
modifier, access to that member is always dynamically dispatched. Because declarations marked with thedynamic
modifier are dispatched using the Objective-C runtime, they’re implicitly marked with the@objc
attribute.
According to Wikipedia:
dynamic dispatch is the process of selecting which implementation of a polymorphic operation (method or function) to call at run time.
Dynamic dispatch is often used in object-oriented languages when different classes contain different implementations of the same method due to common inheritance. For example, suppose you have classes
A
,B
, andC
, whereB
andC
both inherit the methodfoo()
fromA
. Now supposex
is a variable of classA
. At run time,x
may actually have a value of typeB
orC
and in general you can't know what it is at compile time.
Right now, I'm studying the dependency injection framework : Typhoon and when I open the sample project for Swift in all the classes that inherit from the Objective-C class TyphoonAssembly
all the methods relatives to inject dependencies have the dynamic
modifier included in the following way :
public dynamic func weatherReportDao() -> AnyObject {
return TyphoonDefinition.withClass(WeatherReportDaoFileSystemImpl.self)
}
I thought that I'm missing something, but I don't understand where is the polymorphic operation (method or function) to call at run time here.
What's is the purpose of the dynamic dispatch here?
dynamic
keyword, and will work 100% of the time despite what the docs darkly muter about inlining and devirtualization. But here, when they say dynamic, they mean the kind of crazy extra-dynamic runtime shenanigans you can get up to withobjc_msgSend
-type method dispatch. – Maunddynamic
here: http://mjtsai.com/blog/2014/08/18/its-a-coup/ – Unhouse