Unused arguments when destroying an object in Kotlin

When destroying an object, you can only declare the variables that I need?

In this example, I use only b , and my IDE tells me that a not used.

 fun run() { fun makePair() = Pair("Apple", "Orange") val (a, b) = makePair() println("b = $b") } 
+6
source share
3 answers

Since Kotlin 1.1, you can use the underscore to indicate the unused component of the destruction declaration:

 fun run() { fun makePair() = Pair("Apple", "Orange") val (_, b) = makePair() println("b = $b") } 
+9
source

You can use:

 val b = makePair().component2() 
+2
source

If you are only interested in the first couple of arguments, you can omit the rest. This is not possible in your code, but if you change the order of the arguments, you can write it like this:

 fun run() { fun makePair() = Pair("Orange", "Apple") val (b) = makePair() println("b = $b") } 
0
source

All Articles