swift - How to avoid using A.self? -


take code:

protocol p: class { static var hello: string { } }  class a: p {     class var hello: string {         return "hello"     } }  class b: {     override static var hello: string {         return "hello world"     } }  class c: {}  class d: c {     override static var hello: string {         return "hello d"     } }  func sayhello(elements: p.type...) {     p in elements {         print(p.hello)     } }  func sayhelloagain(elements: a.type...) {     p in elements {         print(p.hello)     } }  func sayhellothe3rd(elements: [a.type]) {     p in elements {         print(p.hello)     } }  sayhello(a.self, b.self, c.self) sayhelloagain(a.self, b.self, c.self) 

compare (taken presentation)

func register<t: uitableviewcell t: reusableview, t: nibloadableview>(_: t.type) { ... } tableview.register(foodtableviewcell) 

why have use a.self in 1 case, not in other? , also, don't need use .self when calling 1 argument.

sayhello(a) sayhello(a, b) //doesn't compile 

the .self syntactic salt. it's not necessary technical perspective, exists cause errors in code that's result of typo, such as:

struct foo { }  let foo = foo 

this code give compiler error, telling either need complete initializer/function/method call, or append .self if meant refer type.

in latter example, context deals exclusively types , not values, there's no chance of confusing 1 other, thus, .self isn't necessary.

perhaps there's way modify function declaration in example not require .self, i'm not aware of such feature. i'd interested find out.


Comments