https://kotlinlang.org logo
Title
u

uli

03/10/2017, 4:22 PM
taking the spread operator example mentioned above from stackoverflow:
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:
sum = sumNumbers(Arrays.copyOf(numbers, numbers.length));
Is that on purpose? Does it make sense?
👍 2
📡 1
1
m

mg6maciej

03/10/2017, 4:25 PM
That's an interesting implementation of
sumNumbers
. I can see in my mind you unit tests for that:
sumNumbers(0, 1) == 1
sumNumbers(0, 6, 0, 0, 0, 0) == 6
sumNumbers(-2, 3, 2) == 3
😄 2
u

uli

03/10/2017, 4:44 PM
sorry, i did not mention, that i replaced the original line
return numbers.sum()
from the example with
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
sumNumbers(0)
😇