Since Xcode beta 3, we can write [int]
as short hand for Array<Int>
.
So we should write this:
func commonElements <T, U where T: Sequence, U: Sequence,
T.GeneratorType.Element: Equatable,
T.GeneratorType.Element == U.GeneratorType.Element>
(lhs: T, rhs: U) -> [T.GeneratorType.Element] {
var result = [T.GeneratorType.Element]()
for lhsItem in lhs {
for rhsItem in rhs {
if lhsItem == rhsItem {
result += rhsItem
}
}
}
return result
}
commonElements([1, 2, 3, 4], [2, 4, 6])
For a reason I don't understand, Xcode doesn't accept the short syntax to initialize result
array but accept its long equivalent Array<T.GeneratorType.Element>()
.
Am I missing something?
Maybe the compiler is getting confused. This works though
var result: [T.GeneratorType.Element] = []
and is more readable IMO.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With