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

Loney Chou

09/19/2023, 5:20 AM
Is there any way to "return" fast in when expressions? Don't like the fact that I need another indent for
c
.
Copy code
val foo = when {
    condition1 -> a
    condition2 -> {
        if (condition3) {
            break b // Here
        }
        // Long code
        c
    }
    else -> d
}
j

Jeff Lockhart

09/19/2023, 5:24 AM
Just use an else:
Copy code
val foo = when {
    condition1 -> a
    condition2 -> {
        if (condition3) {
            b // Here
        } else {
            // Long code
            c
        }
    }
    else -> d
}
I'd break out the complexities of "long code" into a separate function as well.
l

Loney Chou

09/19/2023, 5:26 AM
Seems like I can't do `yield b`like Java. 😥
j

Jeff Lockhart

09/19/2023, 5:32 AM
Kotlin doesn't need
yield
because
when
and
if
statements are already expressions. As long as each of your branches produces a proper value for
foo
, the code I just shared works for what you described.
foo
will equal
a
,
b
,
c
, or
d
, depending on the outcome of
condition1
,
condition2
, or
condition3
.
s

sciack

09/19/2023, 5:44 AM
you can also do:
Copy code
val foo = when {
    condition1 -> a
    condition2 && condition3 -> b
    condition2 ->
            // Long code
            c
    else -> d
}
👍 1
also try to keep what is in each condition as small as possible (ideally call a function if you need a long code)
l

Loney Chou

09/19/2023, 5:54 AM
I think I'll go
condition2 && condition3
followed by another
condition2
then. Not hurt that much.
s

sciack

09/19/2023, 7:08 AM
also check IJ suggestion, I found them very useful
k

Klitos Kyriacou

09/19/2023, 8:38 AM
Also possible, but not recommended as it makes the code more obscure:
Copy code
val foo = run {
    when {
        condition1 -> a
        condition2 -> {
            if (condition3) {
                return@run b
            }
            // Long code
            c
        }
        else -> d
    }
}
(Also, it defeats the purpose as it adds an extra level of indentation, which is exactly what you're trying to avoid.)
e

ephemient

09/19/2023, 10:23 AM
you don't need extra indentation if you do it the other way around
Copy code
val foo = when {
    condition1 -> a
    condition2 -> run {
        if (condition3) {
            return@run b
        }
        // Long code
        c
    }
    else -> d
}
👍 3
l

Loney Chou

09/19/2023, 5:16 PM
"`run` ftw" does't quite fit in my taste though...I'm OK with
condition2 && condition3
because it's better than having two possible values yielded in a single when branch. That second
condition2
acts like a fallback so it doesn't break the intent.
k

Klitos Kyriacou

09/19/2023, 5:24 PM
As long as
condition2
doesn't have any side effects and doesn't take a long time to compute, there's no harm in repeating it.
j

Jeff Lockhart

09/19/2023, 5:25 PM
Or compute it before the
when
and just use the boolean result.
w

Wout Werkman

09/20/2023, 8:37 AM
@Jeff Lockhart, that might be too eager in case
condition1
is true 🙂. There is no one size fits all solution
j

Jeff Lockhart

09/20/2023, 3:26 PM
Oh yes, true. Definitely not a one size fits all here.
2 Views