Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is .self after a class name used for? Swift 3

Tags:

class

swift

self

I recently noticed that some codes do init of instances like ClassName.self() for example:

let realm = try! Realm.self()

From the output and the usage thereafter, it is just like as though without it - Realm()

Is there any specific reason or usage for doing an init with .self?

like image 684
Ben Ong Avatar asked Nov 09 '22 04:11

Ben Ong


1 Answers

This is somewhat speculation, but I believe the places that do use Type.self() in the context of Realm follows a misperceived convention that has followed (some irrelevantly) the effects of Swift team resolving the bug

  • SR-899: .self can be omitted if a function has only one parameter

The following commit by the Realm team was in preparation for the anticipated resolution of the bug above:

  • #3712 - explicitly use 'Type.self' notation when passing types to Realm Swift APIs

It's possible that the changes in the commit above (Type updated to Type.self, in proper context) has inspired code bases that make use of Realm to also make use of Type.self in initializer context, i.e., Type.self(). This is, however, a redundant use of the .self suffix.

like image 165
dfrib Avatar answered Nov 15 '22 06:11

dfrib