Home > OS >  Kotlin initializ val with conditions
Kotlin initializ val with conditions

Time:05-31

In c const can be defined by

void func(const int param) {
    const int VALUE = param > 0 ? param : 0;
    ...
}

In Kotlin I am trying to use when:

fun func(param: Int) {
    val VALUE = when(param)
    when(param) {
        param > 0 -> param // Error: expression `param > 0` is not Int
        else -> 0
    }
    ...
}

What is the kotlin way to say expression ? value0 : value1?

CodePudding user response:

Kotlin if one line like this :

 var yourVAr=if(condition)value1 else value2
  • Related