Ternary Operator Similar To ?:

前端 未结 5 1209
小蘑菇
小蘑菇 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:58

    We can combine How to define a ternary operator in Scala which preserves leading tokens? with the answer to Is Option wrapping a value a good pattern? to get

    scala>   "Hi".getClass.getSimpleName |> {x => x.endsWith("$") ? x.init | x}
    res0: String = String
    
    scala> List.getClass.getSimpleName |> {x => x.endsWith("$") ? x.init | x}
    res1: String = List
    

    Is this adequate for your needs?

提交回复
热议问题