taking the spread operator example mentioned above...
# announcements
u
taking the spread operator example mentioned above from stackoverflow:
Copy code
fun sumNumbers(vararg numbers: Int): Int {
    return numbers[1]
}

val numbers = intArrayOf(2, 3, 4)
val sum = sumNumbers(*numbers)
i decompiled the kotlin byte code and found, that the spread operator generates a copy of the numbers array:
Copy code
sum = sumNumbers(Arrays.copyOf(numbers, numbers.length));
Is that on purpose? Does it make sense?
1
📡 1
👍 2
m
That's an interesting implementation of
sumNumbers
. I can see in my mind you unit tests for that:
Copy code
sumNumbers(0, 1) == 1
sumNumbers(0, 6, 0, 0, 0, 0) == 6
sumNumbers(-2, 3, 2) == 3
😄 2
u
sorry, i did not mention, that i replaced the original line
Copy code
return numbers.sum()
from the example with
Copy code
return numers[1]
because i did not want to implement numbers.sum() just for seeing the java code of the call site. Best would be the unit test for
Copy code
sumNumbers(0)
😇