ylemoigne
01/24/2020, 12:25 PMwhen
clause) Eg :
fun main() {
val myShortCutCondition = true
val s = when(2){
1 -> {
if(myShortCutCondition){
// return "shortcut" // for `s` value
}
// longCode
"1"
}
2 -> "2"
else -> ""
}
}
Mike
01/24/2020, 12:30 PMelse
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
.ylemoigne
01/24/2020, 12:33 PM2
is just for exemple sake. I don't want to create another nest level. So I do want multiple return from the 1->{ ...block... }
.ESchouten
01/24/2020, 12:45 PMylemoigne
01/24/2020, 12:48 PMreturn@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 ?
ESchouten
01/24/2020, 12:50 PMBarry Dempsey
01/24/2020, 1:04 PMylemoigne
01/24/2020, 1:10 PMreturn
return from the function. If I was willing to create another function to return from when, I would write :
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"
}
ESchouten
01/24/2020, 1:11 PMKristoffer Andersen
01/24/2020, 1:34 PMfun 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)
}
ylemoigne
01/24/2020, 1:36 PMKristoffer Andersen
01/24/2020, 1:39 PMcall with current continuation
or call/cc
as it's often known:
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)
}
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 🙂Mike
01/24/2020, 1:48 PMwhen(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.ylemoigne
01/24/2020, 2:06 PMreturn@when value
.Mike
01/24/2020, 2:10 PMwhen
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.ylemoigne
01/24/2020, 2:16 PMhow 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...Chantry Cargill
01/24/2020, 4:02 PM