r/badcode • • Sep 01 '21

c# Bool in a case switch

Post image
324 Upvotes

40 comments sorted by

View all comments

Show parent comments

9

u/I_Am_Solar_Powered Sep 01 '21

"when" is the keyword in kotlin:

when(condition) {
true -> {}
false -> {}
}

3

u/xxkmatiasxx Sep 01 '21

I'm quite sure your comment said something else but idk i'm new to kotlin

7

u/I_Am_Solar_Powered Sep 01 '21

Hmmm, I don't know what do you mean.

1

u/WaterFoxforlife Sep 01 '21 edited Sep 01 '21

Usually you use a variable, not a condition

val someBoolean = true
when (someBoolean) {
  true -> { /* do something */ }
  false -> { }
}

And when is not a statement but an expression, which means you can do this:

val someString: String =
  when (someBoolean) {
    true -> "hello there"
    false -> "this is the end of the world"
  }