when can I omit .self

I wrote some codes similar to these:

func printType<T: Any>(one: T.Type) { 
     print(one) 
}

func printType2<T: Any>(one: T.Type, name: String){ 
     print(one) 
}

then I can invoke the first function with or without .self.

printType(Set<Int>) // ok
printType(Set<Int>.self) // ok

but I can not invoke the second one without .self

printType2(Set<Int>.self, name: "name") // ok
printType2(Set<Int>, name: "name") // can not compile

I'm using Xcode Version 7.1 (7B91b)

Answered by OOPer in 83707022

This is a famous undocumented feature of Swift, since 1.0 (or former betas, which I cannot confirm).


When calling a method or function with single type argument, you can omit .self .

I guess this feature is included into Swift to make sizeof-like functions neat, but not sure.

Accepted Answer

This is a famous undocumented feature of Swift, since 1.0 (or former betas, which I cannot confirm).


When calling a method or function with single type argument, you can omit .self .

I guess this feature is included into Swift to make sizeof-like functions neat, but not sure.

when can I omit .self
 
 
Q