spierce7
08/13/2023, 10:17 PMval example: List<String> = when (example) {
// ...
}
.doSomethingAfter()
I really feel like the .doSomethineAfter()
is misplaced and is ugly.
Is there a solution to this that I'm not aware of?Dan Nichols
08/14/2023, 2:51 AMwhen
expression out into a val:
val firstThing = when (example) { ... }
val result = firstThing.doSomethingAfter()
Especially helps readability when firstThing
can be named something semantically meaningfulspierce7
08/15/2023, 2:16 AMChris Lee
08/15/2023, 2:18 AMspierce7
08/15/2023, 2:37 AMChris Lee
08/15/2023, 2:38 AMDan Nichols
08/15/2023, 2:42 AMMohammadreza Khahani
08/15/2023, 5:06 PMlet
function.
val result = when (example) {
//...
}.let { firstThing ->
firstThing.doSomethingAfter()
}
Debdut Saha
08/15/2023, 5:37 PMval result = when(example) {
//....
}.apply { doSomethingAfter() }
Mohammadreza Khahani
08/15/2023, 5:41 PMapply
is more suitable if doSomethingAfter()
doesn't return anything. I assume it returns something after doing the computation. 😀spierce7
08/15/2023, 5:42 PMspierce7
08/15/2023, 5:42 PMChris Lee
08/15/2023, 5:46 PM.also
would be idiomatic to also do some other operation, returning a result.Mohammadreza Khahani
08/15/2023, 5:57 PMval a:Int = listOf("0").let { it.first().toInt() }
val c:List<String> = listOf("").also { it.first().toInt() }
val b:List<String> = listOf("").apply { first().toInt() }
Chris Lee
08/15/2023, 5:58 PM