dimsuz
1
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)
.subscribeOn(Schedulers.computation())
.subscribe({ (list, theme) ->
adapter.setTheme(theme)
adapter.swapData(list)
})
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
Is such kind of syntax planned for Kotlin in future or there are some conserns with it?
2 Likes
orangy
2
Yes, this feature is planned, though exact design is not finalized yet.
2 Likes
dimsuz
3
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)) = this.map { 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) }
3 Likes