https://kotlinlang.org logo
#announcements
Title
# announcements
v

ValV

07/11/2019, 10:04 AM
How to
MutableList<String>
->
Array<String>
?
d

Dias

07/11/2019, 10:05 AM
toTypedArray
🙏 2
v

ValV

07/11/2019, 10:11 AM
Just interesting, why this function was not called simply
toArray
... ?
d

Dias

07/11/2019, 10:12 AM
I would assume to avoid confusion with jvm primitive arrays like DoubleArray
d

diesieben07

07/11/2019, 10:14 AM
Collection
has
toArray
on the JVM which does something different
☝️ 1
that's probably the reason
d

Dias

07/11/2019, 10:14 AM
ah that would make sense too
v

ValV

07/11/2019, 10:15 AM
But in Java's source code it was exactly
toArray
in the place where
toTypedArray
in Kotlin works. I'm confused
k

karelpeeters

07/11/2019, 10:47 AM
They're pretty much equivalent except for the syntax:
toTypedArray
works using reified generics and for
toArray
you need to create and pass the array yourself or live with an
Array<Any>
.
v

ValV

07/11/2019, 11:21 AM
Ah, indeed, there was a call to
.toArray(new String[0])
g

gildor

07/11/2019, 12:12 PM
I believe that the main reason is to be explicit about typed and primitive arrays extensions:
Copy code
val a = listOf(1)
a.toIntArray() // IntArray, extension only for List<Int>
a.toTypedArray() // ArrayInt, extension for List<T>
Collection’s
toArray
is not a problem, because List is not implementing Java Collection in source code (but does on bytecode), so there is no name conflict
4 Views