Search code examples
listkotlinvariadic-functions

Kotlin convert List to vararg


I have input data of type List<UnitWithComponents>

class UnitWithComponents {
    var unit: Unit? = null
    var components: List<Component> = ArrayList()
}

I want to convert the data to a vararg of Unit

Currently I am doing *data.map { it.unit!! }.toTypedArray(). Is there a better way to do it?


Solution

  • 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).