Kotlin convert List to vararg

fun foo(vararg strings: String) { /*...*/ }

Using

foo(strings = arrayOf("a", "b", "c"))

val list: MutableList<String> = listOf("a", "b", "c") as MutableList<String>
foo(strings = list.map { it }.toTypedArray())

Named arguments are not allowed for non-Kotlin functions (*.java)

So, in this case you should replace:

From: strings = list.map { it }.toTypedArray()

To: *list.map { it }.toTypedArray()

GL

Source


No, that's the correct way to do it (assuming you want to throw an exception when it.unit is null for some element of the list).