https://kotlinlang.org logo
#codereview
Title
# codereview
e

Eugen Martynov

04/09/2020, 9:18 AM
Is it idiomatic kotlin?
Copy code
val check = true

when(check) {
    true -> println("it's true")
    false -> println("it's false")
}
m

Mike

04/09/2020, 11:53 AM
For a boolean,
if
is the more readable choice.
When
is better for completeness check on enum/sealed class, or those cases where your
if
has at least 3 branches.
e

Eugen Martynov

04/09/2020, 12:55 PM
That was my assumptions, but is it common sence or written somewhere?
c

Cody Engel

04/09/2020, 1:41 PM
I don't know if it's been written down anywhere but it's implied. One advantage of the language is being concise, using the when is not as concise as say...
Copy code
if (check) println("it's true") else println("it's false")
d

daphillips

04/09/2020, 2:40 PM
3 Views