https://kotlinlang.org logo
#getting-started
Title
# getting-started
k

Kenneth

03/22/2021, 1:42 PM
What is recommended to do when you have a case in a when where you want do to noting? Just empty {}?
m

Mykola Gurov

03/22/2021, 1:49 PM
Or just nothing? Do you mean a lambda for a NOOP (no operation)? I'd usually use empty braces indeed, e.g.
Copy code
val operation: (InputValue)->Unit = {}
t

Tobias Berger

03/22/2021, 2:13 PM
I suggest functionally empty braces with a comment like this:
Copy code
when (a) {
  1 -> doSomething()
  2 -> { /* do nothing */ }
  3 -> throw MyException()
}
2
👍 2
r

Ruckus

03/22/2021, 3:09 PM
I do similar to @Tobias Berger, though I usually put the comment outside the braces:
Copy code
when (a) {
  1 -> doSomething()
  2 -> {} // Do nothing
  3 -> throw MyException()
}
👍 3
e

ephemient

03/22/2021, 4:49 PM
I like writing
Unit
, more than {}, but it works the same
2 Views