In Swift programming language I see an example
func anyCommonElements <T, U where T: Sequence, U: Sequence, T.GeneratorType.Element: Equatable, T.GeneratorType.Element == U.GeneratorType.Element> (lhs: T, rhs: U) -> Bool { for lhsItem in lhs { for rhsItem in rhs { if lhsItem == rhsItem { return true } } } return false }
T.GeneratorType.Element == U.GeneratorType.Element mean that the elements generated by the decomposition of the sequences have the same basic type. Therefore i can do
anyCommonElements("123", "1234") anyCommonElements([1, 2, 3], [1])
but not
anyCommonElements("123", [1, 2])
But T: Sequence, U: Sequence means that the parameters T and U must be sequences, such as String or Array.
How can I write a function that takes two parameters T and U, which must be of the same type using the where clause? Omitting the T: Sequence, U: Sequence requirement results in the error "GeneratorType is not a member of type T"
source share