Questions tagged [kotlin-when]
15 questions
19
votes
5 answers
Kotlin: Using enums with when
Is there any way to cast a when argument to an enum?
enum class PaymentStatus(val value: Int) {
PAID(1),
UNPAID(2)
}
fun f(x: Int) {
val foo = when (x) {
PaymentStatus.PAID -> "PAID"
PaymentStatus.UNPAID -> "UNPAID"
…

serhan
- 213
- 1
- 2
- 5
9
votes
1 answer
How to fix "Non exhaustive 'when' statements on sealed class/interface" in Kotlin
Non exhaustive when statements on sealed class/interface will be prohibited in Kotlin 1.7.
I have a sealed class State and it's children:
sealed class State {
object Initializing : State()
object Connecting : State()
object Disconnecting…

Sergio
- 27,326
- 8
- 128
- 149
9
votes
2 answers
Avoid else condition in 'When' in kotlin
As per documentation of When in Kotlin, else is not mandatory if the compiler knows all the values are covered. This is very in case of emums or sealed class but how to do it in case of arrays for numbers 1 to 5 (startRating).
private fun…

Code_Life
- 5,742
- 4
- 29
- 49
5
votes
2 answers
Calling String method in Kotlin when block
Currently I have a when block like this:
val foo = getStringFromBar()
when {
foo == "SOMETHING" -> { /*do stuff*/ }
foo == "SOMETHING ELSE" -> { /*do other stuff*/ }
foo.contains("SUBSTRING") -> { /*do other other stuff*/ }
else ->…

ColonD
- 954
- 10
- 28
4
votes
1 answer
Can't use if/when assignment to return lambda with inferred parameter but can use if/when blocks
I have function returning a lambda based on an input String condition using if statement, which works fine - using this modified example from Head First Kotlin:
fun getConversionLambda(str: String): (Double) -> Double {
if (str ==…

aneroid
- 12,983
- 3
- 36
- 66
3
votes
3 answers
Kotlin: Omitting enum name when its unambiguous
With Swift enums you can omit the name of the enum in cases where only a value of that type can be used.
So when given the enum (Swift/Kotlin)
enum (class) CompassPoint {
case north
case south
case east
case west
}
Swift only needs the…

xian
- 291
- 2
- 12
3
votes
4 answers
For a final class why does when complain?
So in the following code I get compilation error that "when must be exhaustive add necessary else":
class Test {
}
fun eval(e: Test): Int =
when(e) {
is Test -> throw IllegalArgumentException()
}
To clarify this is…

Jim
- 3,845
- 3
- 22
- 47
1
vote
1 answer
Kotlin. Java break alternative for when construction
I need break like in Java from when branch. From Kotlin 1.7.0 I get error
when expression must be exhaustive
Now I need to add a branch else. Inside else I want to just exit from when.
I can use a return, but in this case, all the code after the…

testivanivan
- 967
- 13
- 36
1
vote
2 answers
How to simplify when expression in kotlin
I'd like to simplify this expression, especially "isDigit" and "isLetter" case. How to do it?
smoothInput.forEach { char ->
when {
char.isValidOperator() -> {
output.push(char)
}
…

halotukozak
- 33
- 4
1
vote
1 answer
Kotlin's when with Pair - complicated contidions
I have complicated bussiness logic conditions which I want to resolve via when statement.
Let's say I have 3 enums Enum1, Enum2 and Enum3 and every has constans A,B,C,D... etc. And depending on value of Enum1 and Enum2 I need to determine list of…

paulonio
- 311
- 1
- 5
1
vote
1 answer
when condition with ignore case in kotlin
I know we can write if, else if, else if with a ignore case.
if (someString.equals("otherString", ignoreCase = true)) {
}
I am very curious about this, how to write a when(in Java it is a switch) condition with ignoring the case.

Shailendra Madda
- 20,649
- 15
- 100
- 138
0
votes
2 answers
How to execute a Function within a When-statement?
I am using the following when-statement in Kotlin:
when(name) {
"Sun" -> print("Sun is a Star")
"Moon" -> print("Moon is a Satellite")
"Earth" -> print("Earth is a planet")
}
And I have a function foo().
This function should be executed…

Michael
- 13,950
- 57
- 145
- 288
0
votes
1 answer
How can kotlin's 'when' expression make a process as this switch-case code?
int num = 100;
char c = 'a';
c = someBigTask(c);
switch (c) {
case 'a':
num += 100;
case 'c':
num += 10;
case 'd':
case 'e':
num += 100;
}
I want to eliminate same code by 'when' expression in another condition like this switch-case…

Rakla i
- 1
- 2
0
votes
2 answers
Check for several type inside when statement in Kotlin
Let's say I've the following:
sealed class Color(val name: String) {
object Red : Color("red")
object Green : Color("green")
object Blue : Color("blue")
object Pink : Color("pink")
object Yellow : Color("yellow")
}
Is it…

Benjamin
- 7,055
- 6
- 40
- 60
-1
votes
1 answer
How can i update 2 lists variables in when expression of kotlin?
fun compareWithIntermediate(output: SHR, input: Intpos): CompareResult{
val matched = mutableListOf()
val mismatched = mutableListOf()
val modifiableAttr = mutableListOf()
compareValue(output.a.b.c,…

I_am_Vits
- 47
- 1
- 10