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

时间:2022-07-14 17:01:09

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? Why do we need it here? It looks like a trivial constraint that's always true.

T = T约束条件是什么?为什么我们需要它?它看起来像一个平凡的约束,总是成立的。

1 个解决方案

#1


6  

The definitions you see in Xcode are not actually a valid Swift code. It is somehow generated on the fly from the original files

您在Xcode中看到的定义实际上不是有效的Swift代码。它是由原始文件生成的。

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,它有时会触发一个警告。

#1


6  

The definitions you see in Xcode are not actually a valid Swift code. It is somehow generated on the fly from the original files

您在Xcode中看到的定义实际上不是有效的Swift代码。它是由原始文件生成的。

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,它有时会触发一个警告。