我已经包装了一条消息,并希望记录我已经包装的消息.
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)