I'm trying to iterate through an array and sum up all the values using generics like so:
func reduceDaArray <T, U>(a: [T], startingValue: U, summed: (U, T) -> U) -> U {
var sum = 0
for number in a {
sum = sum + number
}
return sum
}
reduceDaArray([2,3,4,5,6], 2, +) //(22)
It's giving me the following errors:
Binary operator '+' cannot be applied to operands of type 'Int' and 'A' with regards to the line sum = sum + number
and
Int is not convertible to 'U' with regards to the line return sum
I know this is accomplished better with the reduce method, but I wanted to complete the task using iteration for this instance to get some practice. Why are these errors occurring? I never explicitly stated that T is an Int.