【发布时间】:2014-08-01 15:57:01
【问题描述】:
查看 Swift 中+= 运算符重载的定义,它允许您将元素添加到集合中:
/// Append the elements of rhs to lhs
func +=<T, C : Collection where T == T>(inout lhs: ContiguousArrayBuffer<T>, rhs: C)
^^^^^^
T == T 约束有什么作用?为什么我们在这里需要它?它看起来像一个微不足道的约束,但总是正确的。
【问题讨论】:
-
在这个全局运算符重载函数内部看起来确实很奇怪......但是看看其他使用它的地方,例如
extension T[] : ArrayType {init<S : Sequence where T == T>(_ s: S)}看起来它只是试图确保参数序列的元素与您尝试初始化的数组具有相同的类型。我不确定如何通过这种语法强制执行..
标签: generics collections swift type-constraints