https://kotlinlang.org logo
#announcements
Title
# announcements
s

sreich

04/02/2017, 12:31 AM
intellij is telling me to remove that true because it is superfluous
j

jk

04/02/2017, 10:50 AM
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

sreich

04/02/2017, 11:07 AM
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

jk

04/02/2017, 11:14 AM
it’s because there’s no else here:
Copy code
if (foo()) {
    doStuff()
     true //"this statement is unused"..but why?
}
false
s

sreich

04/02/2017, 11:15 AM
but why?
it's because the last boolean is considered the return, right?
regardless of whichever ones come before
j

jk

04/02/2017, 11:16 AM
yeah that’s right. if you put the else in, the if expression becomes the last statement
s

sreich

04/02/2017, 11:16 AM
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

jk

04/02/2017, 11:19 AM
is it not possible to write it with the else?
Copy code
if (foo()) {
    doStuff()
    true
} else {
    false
}
s

sreich

04/02/2017, 11:23 AM
yeah it is, just more verbose, ya know?
k

kingsley

04/02/2017, 11:59 AM
@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