https://kotlinlang.org logo
y

ylemoigne

01/24/2020, 12:25 PM
Edited question : Is there any syntax that allow for a premature return in when case block ? If a such thing is possible, what it look like ? (Original question : Hello, I have difficulties to find the right syntax (jump label ?) to return from a
when
clause)
Eg :
Copy code
fun main() {
    val myShortCutCondition = true
    val s = when(2){
        1 -> {
            if(myShortCutCondition){
                // return "shortcut" // for `s` value
            }
            
            // longCode
            
            "1"
        }
        2 -> "2"
        else -> ""
    }
}
m

Mike

01/24/2020, 12:30 PM
Use
else
for
"1"
return value. Ideally, don't want multiple
return
in your code, so use the fact that
if
is an expression. But your
when
will always result in
"2"
as you're doing a
when
on the value
2
.
y

ylemoigne

01/24/2020, 12:33 PM
The fixed
2
is just for exemple sake. I don't want to create another nest level. So I do want multiple return from the
1->{ ...block... }
.
e

ESchouten

01/24/2020, 12:45 PM
Easiest would be to warp it in an else statement, indeed. Alternatively you could move the code to a separate function, which would be advisable for // longCode
y

ylemoigne

01/24/2020, 12:48 PM
Yes moving the code in another function is a way. But to be clear, I'm curious about syntax of jump label, the
return@somethine
is this particular case. So I should rephrase the question :
Is there any syntax that allow for a premature return in when case block ? If a such thing is possible, what it look like ?
👍 1
e

ESchouten

01/24/2020, 12:50 PM
I see, not that I am aware of, though I would be interested to know as well
b

Barry Dempsey

01/24/2020, 1:04 PM
I've just used Playground and this works if you want to try it
y

ylemoigne

01/24/2020, 1:10 PM
Thanks you but it's not the same case. Here the
return
return from the function. If I was willing to create another function to return from when, I would write :
Copy code
fun main() {
    val myShortCutCondition = true
    val s = when(2){
        1 -> inCaseOne(myShortCutCondition)
        2 -> "2"
        else -> ""
    }
}

fun inCaseOne(myShortcutCondition:Boolean):String {
    if(myShortcutCondition){
        return "shortcut"
    }
    
    // longcode
    
    return "1"
}
e

ESchouten

01/24/2020, 1:11 PM
@Barry Dempsey the idea is to define the return value of the when statement instead of returning the function it is in You’d expect something like return@when
👌 1
k

Kristoffer Andersen

01/24/2020, 1:34 PM
@ylemoigne You could lift the when into a local function. Now return regains it's short-circuiting power:
Copy code
fun main() {
    val myShortCutCondition = true

    fun whenStmt(): String {
        return when(1){
            1 -> {
                if(myShortCutCondition){
                    return "shortcut"
                }
                // longCode
                "1"
            }
            2 -> "2"
            else -> ""
        }
    }

    val s = whenStmt()
    println(s)
}
y

ylemoigne

01/24/2020, 1:36 PM
Nice tricks I did't think of. Thanks for the suggestion :). (Drawback, it create another nested level. But still more readable than long if .. else in when case).
k

Kristoffer Andersen

01/24/2020, 1:39 PM
What we really want is something like
call with current continuation
or
call/cc
as it's often known:
Copy code
fun main() {
    val myShortCutCondition = true

    val s = call/cc { k ->
        k(when(1) {
            1 -> {
                if (myShortCutCondition) {
                    k("shortcut")
                }
                //longCode
                "1"
            }
            2 -> "2"
            else -> ""
        })
    }

    println(s)
}
call/cc expects a function that takes "the rest of the computation", known as a continuation, as an argument here named
k
, which you can invoke to resume the computation at the point of calling
call/cc
, returning the value supplied to the continuation. But it's not in Kotlin as far as I can tell 🙂
The pseudo-code I just posted is a generalization of the version with a local function declaration.
m

Mike

01/24/2020, 1:48 PM
What you really want is full pattern matching, then. Something like
Copy code
when(value) {
   1 && myShortCutCondition -> "shortcut"
   1 -> "1"
   2 -> "2"
   else -> ""
}
But that's not available, and it sounds like the Kotlin team is reluctant to add it. Perhaps as Java adds more pattern matching to
switch
, we'll see more power in
when
. You could 'inline' value into each of the condition steps, but that does create duplication of code.
👍 1
y

ylemoigne

01/24/2020, 2:06 PM
It seems I would like that because I made a very simple example do illustrate the question. But, what I would really want, is
return@when value
.
m

Mike

01/24/2020, 2:10 PM
Personally, I don't like embedded returns as I find it harder to read the code, but each team has their own preferences. And if the
when
gets too long, then I think it's best to extract it to a function with a meaningful name. If you can't come up with a meaningful name, then it's likely doing too much anyway.
y

ylemoigne

01/24/2020, 2:16 PM
I understand your preference. There is pro and con. Weigth of each vary between context. That's why I rephrased the question to focus on the syntax part (does it exist, or not). Because, you know, it's internet, you ask for
how I do XXX with kotlin ?
and there is always some to say
yeah, you really shouldn't use kotlin for this, here is how to do it in Haskell : ...
And then, that's not what your asked for. And you feel bad because the person wanted to help, but you have to say
Hey thanks for your effort but that was not the question
which is a bit harsh...
👍 1
c

Chantry Cargill

01/24/2020, 4:02 PM
I tend to branch to null and then null coalesce.
4 Views