Swift的GeneratorOf< T>疯狂的初始化 [英] Swift's GeneratorOf<T> crazy init
问题描述
GeneratorOf创建一个通用的生成器
(和序列
):
struct GeneratorOf< T> :Generator,Sequence {
init(_ next:() - > T?)
init< G:Generator where T == T>(_ self_:G)
func next ) - > T?
func generate() - > GeneratorOf< T>
}
我不明白的是第二个init:
init< G:Generator where T == T>(_ self_:G)
pre>
为什么在地球上需要说明
T == T
??
这是
(_ self_:G)
是什么意思?
谢谢Swift的主要优点之一是比Objective C更简单的语法: - P
解决方案Swift stdlib头自动生成器中的一个错误。当解析类型时,它们在某些地方替换等价类型,它们不应该。这里的实际定义应该是:
init< G:Generator where Generator.Element == T>(_ self_: G)
但由于
Generator.Element
与T
相同,该工具将替换它。苹果一直在努力解决这个问题。
(_ self_:G)
是标准语法,我没有看到在别处使用,除了SequenceOf
,这可能是由同一个人写的。我怀疑这是开发人员命名的变量,而不是有一些更深层次的意义。
记住,stdlib头并不是一个正常头。 Swift没有标题。 Apple
GeneratorOf creates a generic
Generator
(andSequence
):struct GeneratorOf<T> : Generator, Sequence { init(_ next: () -> T?) init<G : Generator where T == T>(_ self_: G) func next() -> T? func generate() -> GeneratorOf<T> }
The line I don't understand is the second init:
init<G : Generator where T == T>(_ self_: G)
Why on Earth is it necessary to state that
T == T
??What is this
(_ self_: G)
supposed to mean??Thanks goodness that one of Swift major advantages is a much simpler syntax than Objective C... :-P
解决方案This is a bug in the Swift stdlib header auto-generator. When resolving types, they replace equivalent types in some places they shouldn't. The actual definition here is supposed to be:
init<G : Generator where Generator.Element == T>(_ self_: G)
But since
Generator.Element
is the same asT
, the tool replaces it. Apple's been working on fixing that.
(_ self_: G)
is standard syntax, though an admittedly unusual style that I haven't seen used elsewhere, except for inSequenceOf
, which was probably written by the same person. I suspect it's just how the developer named the variable, rather than having some deeper meaning.Remember, the stdlib header isn't a "normal" header. Swift doesn't have headers. Apple does a special Apple-only trick to allow one.
Chris Lattner commented on this in the devforms.
这篇关于Swift的GeneratorOf< T>疯狂的初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!