Skip to content

Instantly share code, notes, and snippets.

@dladukedev
Created December 20, 2023 20:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dladukedev/3394a2d02ae5d56fc7bddbd026799955 to your computer and use it in GitHub Desktop.
Save dladukedev/3394a2d02ae5d56fc7bddbd026799955 to your computer and use it in GitHub Desktop.
Exhaustive if blocks in Kotlin
import kotlin.random.Random
val myInt = Random.nextInt()
val myBool = Random.nextBoolean()
// Expression - Exhaustive Required
val result = if (myInt == 1) {
"I'm #1"
} else {
"I'm something else"
}
// Statement - Exhaustive Not Required
if (myInt == 1) {
print("I'm #1!")
}
// Exhaustive not Assumed
val nextResult = if (myBool == true) {
"This is true"
} else if (myBool == false) {
"This is false"
} else {
// Required even if not possible
"Invalid case"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment