How can I match classes in a Scala “match” statement?

前端 未结 4 1884
孤独总比滥情好
孤独总比滥情好 2021-01-01 14:43

How can I use a \"match\" statement to identify the value of a class variable? The following is invalid, and I can\'t find an acceptable variant -- other than if ... else i

4条回答
  •  孤城傲影
    2021-01-01 14:54

    To consider inheritance:

    val what = c match {
      case q if classOf[Int].isAssignableFrom(q) => "int!"
      case q if classOf[Float].isAssignableFrom(q)  => "float!"
    }
    

提交回复
热议问题