Last active
July 28, 2021 19:19
-
-
Save Takhion/fd0f3c04706414689f54a85a5255f241 to your computer and use it in GitHub Desktop.
Exhaustive `when` mapping in Kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@file:Suppress("PackageDirectoryMismatch") // root package looks great when importing, ie. `import exhaustive` | |
/** | |
* Allows requiring an exhaustive mapping in a `when` block when used with the [rangeTo] operator. | |
* @sample [exhaustive_when_example] | |
*/ | |
@Suppress("ClassName") // lowercase because it should look like a keyword | |
object exhaustive { | |
/** @see [exhaustive] */ | |
@JvmStatic @Suppress("NOTHING_TO_INLINE") // static & inline so Proguard can remove it while optimizing | |
inline operator fun <T> rangeTo(whenBlock: T): T = whenBlock | |
} | |
private enum class SomeEnum { a, b, c } | |
private fun exhaustive_when_example(enum: SomeEnum) { | |
exhaustive..when (enum) { | |
SomeEnum.a -> Unit | |
SomeEnum.b -> println() | |
SomeEnum.c -> readLine() | |
} | |
} |
@Jswizzy: This way of doing it is much faster
val <T> T.exhaustive: T get() = this
The main issue with that approach is that it pollutes the global namespace, making the extension appear on every type, which can be very confusing/misleading. In addition, the exhaustive
keyword should really be placed as close as possible to the when
, instead of at the end of a (potentially long) block.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This way of doing it is much faster
val <T> T.exhaustive: T get() = this