I have a function with a vararg parameter. This vararg parameter needs to be passed to another function as a list.
How do I convert the vararg param into a list? listOf()
gave me an error.
fun insertMissingEntities(vararg entities: Entity) {
val list = listOf(entities)
passMissingEntities(list) // Type mismatch.
// Required: List<Entity>
// Found: List<Array<out Entity>>
}
You can use the extension function .asList()
, which does no added copying (unlike listOf
with the spread operator).
fun insertMissingEntities(vararg entities: Entity) {
passMissingEntities(entities.asList())
}
The vararg param needs to be spread with the spread *
operator.
fun insertMissingEntities(vararg entities: Entity) {
val list = listOf(*entities)
passMissingEntities(list)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With