Jean-Philippe Pellet
Jean-Philippe Pellet

Reputation: 59994

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:

/// 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.

Upvotes: 12

Views: 394

Answers (1)

Sulthan
Sulthan

Reputation: 130102

The definitions you see in Xcode are not actually a valid Swift code. 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.

I have tried to define a similar function by myself and T == T is not necessary there, it actually sometimes triggers a warning.

Upvotes: 6

Related Questions