Created
December 20, 2023 20:02
-
-
Save dladukedev/3394a2d02ae5d56fc7bddbd026799955 to your computer and use it in GitHub Desktop.
Exhaustive if blocks 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
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