When else in Kotlin does nothing
Nov 6, 2023
I just wanted to share a quick note on some ways we handle the case where the else branch has to do… NOTHING.
Sometime you find yourself in a situation where you open a when expression but you only care about certain branches. What happens with the else branch?
Some people just open and close brackets:
else -> {}
Some others use Unit:
else -> Unit
Others get really fancy with their comments:
else -> {/* Do nothing*/}
A nicer way to do this is just take the advantage of typealiases, this way we have the same logic as using Unit and the readability of a comment line:
typealias DoNothing = Unit
This way we can use it like this:
else -> DoNothing
And that’s it, just a quick tip for the common android developer.