intellij is telling me to remove that true because...
# announcements
s
intellij is telling me to remove that true because it is superfluous
j
it’s probably telling you it’s superfluous because it’s unnecessary to write an if statement when
bar()
returns a boolean - the when could be written this to achieve the same result:
Copy code
when(blah) {
    Thing.Value -> bar()
}
s
nope. that isn't it
i still don't understand this.
Copy code
fun bizz(a: Int) = when (a) {
            1 -> {
                bar()
                true
            }

            2 -> {
                if (foo()) {
                   doStuff()
                    true //"this statement is unused"..but why?
                }
                    false
            }
           else -> true
j
it’s because there’s no else here:
Copy code
if (foo()) {
    doStuff()
     true //"this statement is unused"..but why?
}
false
s
but why?
it's because the last boolean is considered the return, right?
regardless of whichever ones come before
j
yeah that’s right. if you put the else in, the if expression becomes the last statement
s
ok, makes sense. too bad there wasn't a way to specify you want it to be the last one. but return isn't allowed, so..
j
is it not possible to write it with the else?
Copy code
if (foo()) {
    doStuff()
    true
} else {
    false
}
s
yeah it is, just more verbose, ya know?
k
@sreich the last statement is considered return. Yes. However, if you need to return explicitly. You should be able to
return@when
. Not sure if that works currently. Or you could just put a label at the when and
return@label