In Java it is said that varargs are just syntactic...
# getting-started
f
In Java it is said that varargs are just syntactic sugar for arrays. Is this also true for Kotlin or can it do anything arays can't?
j
In Kotlin, it's syntactic sugar specifically for Array<out X>, at least for class types. I'm not sure if the primitive types are handled specially; I'd have to check.
f
yes they are using the typed arrays
so theoretically an array could do anything a vararg parameter can do?
c
You can use the spread operator to populate varargs from another array, which you can’t do in Java (see last paragraph of https://kotlinlang.org/docs/reference/functions.html#variable-number-of-arguments-varargs) Otherwise, it’s just a plain array, there’s nothing extra you can do with it
f
thank you
j
Wow, it's been so long since I used Java I forgot there's no spread operator. Yeah, the spread operator is great.
k
You can "spread" in Java too, it just happens automatically and without an array copy.
d
Well, the main advantage of the spread operator is that you can combine arrays, single-element things all into one vararg parameter, which Java cannot do natively.
☝️ 2
f
yea I'm looking that up right up. Seems like Kotlin avoids a lot of problems with this operator