Ternary Operator Similar To ?:

前端 未结 5 1211
小蘑菇
小蘑菇 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条回答
  •  旧时难觅i
    2020-12-07 18:45

    Rex Kerr’s answer expressed in basic Scala:

    "Hi".getClass.getSimpleName match {
      case x if x.endsWith("$") => x.init
      case x => x
    }
    

    although I’m not sure what part of the if–else construct you want to optimise.

提交回复
热议问题