简体   繁体   English

这种泛型类型约束在Swift中意味着什么?

[英]What does this generic type constraint mean in Swift?

Look at the definition for the overload of += operator in Swift that lets you add elements to a collection: 查看Swift中+=运算符重载的定义,它允许您向集合中添加元素:

/// Append the elements of rhs to lhs
func +=<T, C : Collection where T == T>(inout lhs: ContiguousArrayBuffer<T>, rhs: C)
                                ^^^^^^

What does the T == T constraint do? T == T约束有什么作用? Why do we need it here? 我们为什么需要它? It looks like a trivial constraint that's always true. 它看起来像一个微不足道的约束,总是如此。

The definitions you see in Xcode are not actually a valid Swift code. 您在Xcode中看到的定义实际上并不是有效的Swift代码。 It is somehow generated on the fly from the original files 它以某种方式从原始文件中动态生成

I would assume that the T == T part is a mistake done by the generator when reducing the original files. 我会假设T == T部分是生成器在减少原始文件时所犯的错误。

I have tried to define a similar function by myself and T == T is not necessary there, it actually sometimes triggers a warning. 我试图自己定义一个类似的函数,那里没有必要T == T ,它实际上有时会触发警告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM