This is possible by creating an operator component2
as an extension method:
operator fun <T> List<T>.component2(): List<T> = drop(1)
fun destrcutList() {
val (first: String, second: List<String>) = listOf("1", "2", "3")
}
You need to create an extension method only component2
, component1
to be used as before.
Types may be omitted:
fun destrcutList() {
val (first, second) = listOf("1", "2", "3")
println(second[0]) // prints "2"
}
One important note: if you declare an extension method in another package , you need to manually import the function :
import your.awesome.package.component2
source
share