This is a noob question and may have been asked be...
# getting-started
s
This is a noob question and may have been asked before, but what does the when block do by default if a case is not found? Like in the following example, what is actually happening when I pass
BAD
into that function? Essentially, I want to make the when block exhaustive but not change the behavior of the function.
Copy code
enum class SomeEnum {
    GOOD,
    BAD
}

fun doTheEnumThing(someEnum: SomeEnum) {
            when (someEnum) {
                SomeEnum.GOOD -> println("good")
            }
}
r
To answer your question, nothing. It works the same as
Copy code
fun doTheEnumThing(someEnum: SomeEnum) {
    if (someEnum == SomeEnum.GOOD) {
        println("good")
    }
}
There's no else statement, so nothing happens. It just returns.
👍 2
s
What I'm trying to figure out is like.. what would I put in the BAD case so that it has the same behavior? Just something like this?
Copy code
when (someEnum) {
                SomeEnum.GOOD -> println("good")
                SomeEnum.BAD -> return
            }
r
If there isn't a bad case, why do you want to add a stub for one? But if you do want it, you could do
Copy code
SomeEnum.BAD -> Unit
// or
SomeEnum.BAD -> {}
👍 1
p
that would return out of the function which might not be what you want
r
@pavel I assume that was meant for @Steven McLaughlin's code block and not mine, correct?
p
yes
👍 1
s
Cool. I mostly want to stub one because the ide is giving me warnings haha
r
You should probably also add a comment to make it clear it wasn't a mistake
Copy code
SomeEnum.BAD -> Unit  // Do Nothing
// or
SomeEnum.BAD -> {}  // Do Nothing
👍 1
c
Note that if your
when
is a value for an expression, the compiler will mandate exhaustiveness