Reputation: 1948
I've wrote some functions (Swift 2.1, XCode 7.1.1):
public func test1<T:UIView>(type: T.Type) -> T {
print(type.dynamicType)
return type.init()
}
public func test2<T:UIView>(type: T.Type)(_ n: Int) -> T {
print(type.dynamicType)
return type.init()
}
public func test3<T1:UIView, T2:UIView>(type1: T1.Type, _ type2: T2.Type) {
print(type1.init())
print(type2.init())
}
public func test4<T:UIView>(type: T.Type, _ n: Int) {
print(type.init())
print(n)
}
public func test5<T:UIView>(n: Int,_ type: T.Type) {
print(type.init())
print(n)
}
And call them with:
test1(UIButton)
test1(UIButton.self)
test2(UIButton)(1)
test2(UIButton.self)(1)
test3(UIButton.self, UITextField.self)
test4(UIButton.self, 1)
test5(1, UIButton.self)
As you can see, when a type is the only parameter, it can omit the ".self". But for all functions that not have only a type parameter, they require the ".self".
I want to know:
Upvotes: 1
Views: 494
Reputation: 9262
Coincidentally, this just came up on swift-evolution. The ability to omit .self
when the Type is the only argument, has been reported as a bug in Swift.
The relevant quote from Apple:
It's a bug.
.self
is supposed to be required everywhere. Unfortunately, we've had the bug for a while, so I'm not sure how much code we'd break by changing it. If we wanted to remove the requirement, that would be considered a language change and would have to go through the Swift Evolution Process.
Upvotes: 2