This is specially common on Android, with so many addOnSomethingListener being called from everywhere. Is there a way to make nested lambdas like this more concise? Like so:
The issue here I think is with receivers and parameters. The trouble is that currently in Kotlin you canât just simply compose 2 functions that take lambdas together like that because what do you do for parameters? Youâd have to define way to many functions to define those lambdas in a typed fashion.
Like broot said, I think maybe this style should be considered idiomatic for conciseness sake:
i donât see any reason why you couldnât simply make a context class and use an extension function for something like this, or perhaps even just an extension function.
iâm not sure your exact use case here but there could be a way of generalizing this so you only need to write it once or twice and use it wherever needed
You can rename then2 to something more meaningful and provide alternative âthensâ for passing results between functions, so e.g. result of doSomething() is passed as a parameter to doSomethingElse(). Still, I donât consider this a good idea
Omg I actually am that guy and I actually implemented such âthenâ fun. Even worse, it had three generic parameters because it allowed chaining mapping operations.
And then I had to hand-over the codebase to clueless JAVA devs who asked questions like âwhat are unit testsâ or âhow do I switch branchâ (company decided to cut costs by hiring twice as many devs which were 7 times cheaper each). The best day in my career XD.