Ternary Operator Similar To ?:

前端 未结 5 1212
小蘑菇
小蘑菇 2020-12-07 18:11

I am trying to avoid constructs like this:

val result = this.getClass.getSimpleName
if (result.endsWith(\"$\")) result.init else result

Ok,

5条回答
  •  天涯浪人
    2020-12-07 18:57

    Since if-else constructions in Scala return a value, you can use this

    val a = if (1 < 0) 1 else 2
    

    More info: https://alvinalexander.com/scala/scala-if-then-ternary-operator-cookbook-examples

提交回复
热议问题