java - Switch/When without break in Kotlin -




this question has answer here:

i'm new kotlin , want switch don't have 'break'. in java that:

    switch (b){         case 3:             log.d("int", "3");         case 2:             log.d("int", "2");         case 1:             log.d("int", "1");     } 

and if b = 2 print:

d/int: 2

d/int: 1

i want in kotlin without repeting same code each case. in kotlin convertor, code output that:

when (b) {   3 -> {     log.d("int", "3")     log.d("int", "2")     log.d("int", "1")   }   2 -> {     log.d("int", "2")     log.d("int", "1")   }   1 -> log.d("int", "1") } 

is there way of doing that? actual code way bigger that;

what you're asking "fall-through", not supported in kotlin. actually, it's not design use imho of course depends on use case.

if want it, can extract code blocks executed in cases in (local) functions if like:

fun fallthrough(myint: int) {      fun case3() = println("1")      fun case2() {         case3()         println("2")     }      fun case1() {         case2()         println("3")     }      when (myint) {         3 -> case1()         2 -> case2()         1 -> case3()     } } 




wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -

python - Read npy file directly from S3 StreamingBody -