In swift, an instance func
can't call a static/class func
without prefixing the method call with the class name. OR you can use type(of: self)
, e.g
class Foo { static func doIt() { } func callIt() { Foo.doIt() // This works type(of: self).doIt() // Or this doIt() // This doesn't compile (unresolved identifier) } }
My question is, what's the difference here? Is it just a matter of coding style, or is there some difference e.g. static or dynamic dispatch going on?
If it is just coding style, what's the preferred style?
You can call static methods on self , because self is an instance of the class that has the static method, and thus has the method. You can also call static methods on classes directly, because a static method doesn't require an object instance as a first argument - which is the point of the static method.
Static methods have keyword "static" before the method name, belong to the class and not the instance, and can be accessed through the class name.
When self is accessed in a type method ( static func or class func ), it refers to the actual type (rather than an instance). Swift allows to omit self when you want to access instances properties.
The metatype that you call the static method on is available to you in the method as self (it's simply passed as an implicit parameter). Therefore if you call doIt() on type(of: self) , self will be the dynamic metatype of the instance. If you call it on Foo , self will be Foo.
There are two main differences.
self
inside the static methodThe metatype that you call the static method on is available to you in the method as self
(it's simply passed as an implicit parameter). Therefore if you call doIt()
on type(of: self)
, self
will be the dynamic metatype of the instance. If you call it on Foo
, self
will be Foo.self
.
class Foo { static func doIt() { print("hey I'm of type \(self)") } func callDoItOnDynamicType() { type(of: self).doIt() // call on the dynamic metatype of the instance. } func classDoItOnFoo() { Foo.doIt() // call on the metatype Foo.self. } } class Bar : Foo {} let f: Foo = Bar() f.callDoItOnDynamicType() // hey I'm of type Bar f.classDoItOnFoo() // hey I'm of type Foo
This difference can be really important for factory methods, as it determines the type of instance you create.
class Foo { required init() {} static func create() -> Self { return self.init() } func createDynamic() -> Foo { return type(of: self).create() } func createFoo() -> Foo { return Foo.create() } } class Bar : Foo {} let f: Foo = Bar() print(f.createDynamic()) // Bar print(f.createFoo()) // Foo
(Martin has already covered this, but I thought I would add it for the sake of completion.)
For class
methods that are overridden in subclasses, the value of the metatype that you call the method on determines which implementation to call.
If called on a metatype that is known at compile time (e.g Foo.doIt()
), Swift is able to statically dispatch the call. However, if you call the method on a metatype that isn't known until runtime (e.g type(of: self)
), the method call will be dynamically dispatched to the correct implementation for the metatype value.
class Foo { class func doIt() { print("Foo's doIt") } func callDoItOnDynamicType() { type(of: self).doIt() // the call to doIt() will be dynamically dispatched. } func classDoItOnFoo() { Foo.doIt() // will be statically dispatched. } } class Bar : Foo { override class func doIt() { print("Bar's doIt") } } let f: Foo = Bar() f.callDoItOnDynamicType() // Bar's doIt f.classDoItOnFoo() // Foo's doIt
For a class
method it makes a difference if the method is overridden in a subclass:
class Foo { class func doIt() { print("Foo doit") } func callViaClassname() { Foo.doIt() } func callViaTypeOf() { type(of: self).doIt() } } class Bar: Foo { override class func doIt() { print("Bar doit") } } Bar().callViaClassname() // Foo doit Bar().callViaTypeOf() // Bar doit
This is also documented in "Types" in the Swift Language Reference:
You can use a
type(of:)
expression with an instance of a type to access that instance’s dynamic, runtime type as a value, ...
I don't know a difference for a Correction: See Hamish's answer for the difference in both static and class methods.static
method (which is final
and cannot be overridden in a subclass).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With