When destructuring an object, is it possible to only declare the variables I need?
In this example I'm only using b
and my IDE is giving me a warning that a
is unused.
fun run() {
fun makePair() = Pair("Apple", "Orange")
val (a, b) = makePair()
println("b = $b")
}
Since Kotlin 1.1, you can use an underscore to mark an unused component of a destructing declaration:
fun run() {
fun makePair() = Pair("Apple", "Orange")
val (_, b) = makePair()
println("b = $b")
}
You could use:
val b = makePair().component2()
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