相关疑难解决方法(0)

如何获得_的类:任何

我已经包装了一条消息,并希望记录我已经包装的消息.

val any :Any = msg.wrappedMsg
var result :Class[_] = null
Run Code Online (Sandbox Code Playgroud)

我能找到的唯一解决方案是匹配所有内容:

result = any match {
  case x:AnyRef => x.getClass
  case _:Double => classOf[Double]
  case _:Float  => classOf[Float]
  case _:Long   => classOf[Long]
  case _:Int    => classOf[Int]
  case _:Short  => classOf[Short]
  case _:Byte   => classOf[Byte]
  case _:Unit   => classOf[Unit]
  case _:Boolean=> classOf[Boolean]
  case _:Char   => classOf[Char]
}
Run Code Online (Sandbox Code Playgroud)

我想知道是否有更好的解决方案?以下两种方法不起作用:(

result = any.getClass //error
// type mismatch;  found   : Any  required: ?{val getClass: ?} 
// Note: Any is not implicitly converted to …
Run Code Online (Sandbox Code Playgroud)

types type-systems scala class scala-2.8

4
推荐指数
1
解决办法
4573
查看次数

标签 统计

class ×1

scala ×1

scala-2.8 ×1

type-systems ×1

types ×1