Destructuring in lambdas


Wouldn't it be nice to be able to use destructuring in lambdas?

Consider the following code, where ‘subscribe’ receives an instance of Pair (emitted by the observable):

    val subscription = AndroidObservable

           .bindActivity(this, observable)
           .subscribe({ (list, theme) ->

Currently Kotlin does not support this and I have to use something like

.subscribe({ data ->
  val (list,theme) = data

which isn’t too bad, but still I’d like it to be even better :slight_smile:

Is such kind of syntax planned for Kotlin in future or there are some conserns with it?


Yes, this feature is planned, though exact design is not finalized yet.


Nice to know, thanks! Please mention it in some blog post when it's ready ;)


I implemented this using extension functions:

inline fun <A,B,R> List<Pair<A,B>>.forEach( op : ((A,B) -> R)) = this.forEach { op(it.first,it.second) }
inline fun <A,B,R> List<Pair<A,B>>.map( op : ((A,B) -> R)) = { pair -> op(pair.first,pair.second) }
inline fun <A,B> List<Pair<A,B>>.filter( op : ((A,B) -> Boolean)) = this.filter { pair -> op(pair.first,pair.second) }


I wrote these extension functions a few weeks ago:

inline fun <K, V> Iterable<Map.Entry<K, V>>.forEachEntry(action: (K, V) -> Unit) =
    forEach { action(it.key, it.value) }

inline fun <A, B> Iterable<Pair<A, B>>.forEachPair(action: (A, B) -> Unit) =
    forEach { action(it.first, it.second) }