Is there a way to do nested destructuring?

Is there a way to do nested destructuring?

There are two places where it comes up quasi-frequently for me: lambda arguments, and unwrapping data classes.

Example 1:


data class Foo (val s: String, val i: Int) data class Bar (val f: Foo) fun doStuff () { val (f) = makeBar() val (_, i) = f useI(i) } fun useI(i: Int) { TODO() } fun makeBar(): Bar { TODO() }

I’d like it if I could’ve written

val ((_, i)) = makeBar()

instead of having

f

as an intermediate.

Example 2:

data class Foo (val s: String, val i: Int)


fun doStuff () { val sum = makeListOfFoo().fold(0) { acc, foo -> acc + foo.i } println(sum) } fun makeListOfFoo(): List<Foo> { TODO() }

I’d like it if I could write my lambda args like

fold(acc) { acc, (_, i) ->

None of that is possible, right?

submitted by /u/ragnese
[link] [comments]