在 Swift 标准库的协议中使用 typealias 语法
Use of typealias syntax within protocols in the Swift standard library
在 Apple 的 Swift 编程指南中,描述了如何在协议中使用 typealias 关键字(来自泛型部分)
protocol Container {
typealias ItemType
mutating func append(item: ItemType)
var count: Int { get }
subscript(i: Int) -> ItemType { get }
}
然后执行:
struct IntStack: Container {
typealias ItemType = Int // can sometimes be left out and inferred by the compiler
mutating func append(item: Int) {
self.push(item)
}
// redacted
}
但是,在 Swift 标准库中发现了一个明显不同的用例,例如
public protocol ForwardIndexType : _Incrementable {
typealias Distance : _SignedIntegerType = Int
// redacted
}
或
public protocol CollectionType : Indexable, SequenceType {
typealias Generator : GeneratorType = IndexingGenerator<Self>
public func generate() -> Self.Generator
// redacted
}
连同:
extension CollectionType where Generator == IndexingGenerator<Self> {
public func generate() -> IndexingGenerator<Self>
}
这个语法代表什么?似乎类型别名同时被声明、限制(例如生成器类型)和分配?这是什么意思,为什么会这样?我希望仅在实现客户端代码时看到赋值 (=)。
我对 typealias 的理解是它表示一个类型,即 'filled in' 通过实现代码(根据泛型)但是这里它似乎在声明中为 typealias 实现了一个类型,即使这也在扩展中完成(我期望的地方)。
看看。使用冒号表示继承,使用等号表示赋值。
根据我的理解,这意味着以下内容:
typealias X // defines associated type X for subclasses to override
typealias X: Y // defines associated type X and requires that it conform to Y
typealias X = Z // defines associated type X with a default of type Z
typealias X: Y = Z // defines associated type X with a default of type Z and requires that any overrides conform to Y
我的解释似乎得到 this article 关于 Swift 泛型的支持:
An associated type is declared by a protocol using the typealias
keyword. It normally set by an item that conforms to that protocol,
although you can supply a default. Like type parameters, an
associated type can be used as a token when building generic type
rules.
使用关键字 typealias
可能会误导定义关联类型,可能会在 the future 中用 associatedtype
代替。
在 Apple 的 Swift 编程指南中,描述了如何在协议中使用 typealias 关键字(来自泛型部分)
protocol Container {
typealias ItemType
mutating func append(item: ItemType)
var count: Int { get }
subscript(i: Int) -> ItemType { get }
}
然后执行:
struct IntStack: Container {
typealias ItemType = Int // can sometimes be left out and inferred by the compiler
mutating func append(item: Int) {
self.push(item)
}
// redacted
}
但是,在 Swift 标准库中发现了一个明显不同的用例,例如
public protocol ForwardIndexType : _Incrementable {
typealias Distance : _SignedIntegerType = Int
// redacted
}
或
public protocol CollectionType : Indexable, SequenceType {
typealias Generator : GeneratorType = IndexingGenerator<Self>
public func generate() -> Self.Generator
// redacted
}
连同:
extension CollectionType where Generator == IndexingGenerator<Self> {
public func generate() -> IndexingGenerator<Self>
}
这个语法代表什么?似乎类型别名同时被声明、限制(例如生成器类型)和分配?这是什么意思,为什么会这样?我希望仅在实现客户端代码时看到赋值 (=)。
我对 typealias 的理解是它表示一个类型,即 'filled in' 通过实现代码(根据泛型)但是这里它似乎在声明中为 typealias 实现了一个类型,即使这也在扩展中完成(我期望的地方)。
看看
根据我的理解,这意味着以下内容:
typealias X // defines associated type X for subclasses to override
typealias X: Y // defines associated type X and requires that it conform to Y
typealias X = Z // defines associated type X with a default of type Z
typealias X: Y = Z // defines associated type X with a default of type Z and requires that any overrides conform to Y
我的解释似乎得到 this article 关于 Swift 泛型的支持:
An associated type is declared by a protocol using the typealias keyword. It normally set by an item that conforms to that protocol, although you can supply a default. Like type parameters, an associated type can be used as a token when building generic type rules.
使用关键字 typealias
可能会误导定义关联类型,可能会在 the future 中用 associatedtype
代替。