What is the reason that there is only a spread ope...
# announcements
r
What is the reason that there is only a spread operator for
Array
but not for `Collection`/`Iterable`?
j
you can just call
.toTypedArray()
on a collection, then spread it
r
not always unfortunately:
Copy code
fun <T> foo<T>(l: List<T>) = bar(*l.toTypedArray())
Does not work since T has to be reified (Kotlin needs to know what type the array has)
k
Spread doesn't really make sense for collections, since it just means "passing an array as a vararg parameter" on the JVM.
r
I guess in the end it boils down to the fact that you need to know the type in order to pass it as an array and since you don't always know it, you cannot provide a spread operator in all cases. But then... wouldn't it be possible to provide an operator overload with an reified T. I guess you could. So back to the beginning 🙂 why only array?
k
Because Lists aren't a language primitive, and providing a dedicated operator for them is ugly.
r
so the following should be forbidden:
listOf(1) + listOf(2)
? 😉
k
I was careful to include "dedicated":
+
is a general overloadable operator.
r
I see 🙂, fair enough I guess