Here is the code I write:
下面是我写的代码:
func printType<T: Any>(one: T.Type) {
print(one)
}
func printType2<T: Any>(one: T.Type, name: String) {
print(one)
}
printType(Set<Int>)
printType2(Set<Int>.self, name: "name")
I wonder why printType(Set<Int>)
can work. And in printType2(Set<Int>.self, name: "name")
, I can not leave out .self to make it work.
我想知道为什么printType(Set
Edit:
编辑:
I want to know which rule imply when I can omit .self
. I had tried to find it in Apple official references, but failed.
我想知道当我可以省略。self时,这条规则意味着什么。我曾试图在苹果官方的推荐信中找到它,但失败了。
Edit:
编辑:
I test the codes in Xcode Version 7.1 (7B91b).
我在Xcode版本7.1 (7B91b)中测试代码。
1 个解决方案
#1
1
I also ask it in apple official forum, and get an answer there:
我也在苹果官方论坛上问过这个问题,得到的答案是:
This is a famous undocumented feature of Swift, since 1.0 (or former > betas, which I cannot confirm).
这是自1.0(或前> betas,我无法确认)以来Swift的一个著名的未公开的特性。
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.
当使用单个类型参数调用方法或函数时,可以省略> .self。我猜Swift中包含了这个特性,以使类似于sizeof的函数>更简洁,但不确定。
https://forums.developer.apple.com/thread/24980
https://forums.developer.apple.com/thread/24980
#1
1
I also ask it in apple official forum, and get an answer there:
我也在苹果官方论坛上问过这个问题,得到的答案是:
This is a famous undocumented feature of Swift, since 1.0 (or former > betas, which I cannot confirm).
这是自1.0(或前> betas,我无法确认)以来Swift的一个著名的未公开的特性。
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.
当使用单个类型参数调用方法或函数时,可以省略> .self。我猜Swift中包含了这个特性,以使类似于sizeof的函数>更简洁,但不确定。
https://forums.developer.apple.com/thread/24980
https://forums.developer.apple.com/thread/24980