Zhi*_*Han 3 scala pattern-matching type-erasure
我试图获得一种类型安全的方法来转换解析JSON字符串的结果.我想检查一个字段是Map [String,any]还是普通字符串.我的第一次尝试是
def test(x:Any) = {
x match {
case m:Map[String,Any] => ...
...
}
Run Code Online (Sandbox Code Playgroud)
这导致"类型模式Map [String,Any]中的非变量类型参数String未被选中,因为它被擦除消除了"
通过TypeTag和ClassTag的文档,我找不到一个很好的方法来实现它.以下代码不会导致警告,但我想知道为什么它会起作用.
type StringMap = Map[String,Any]
def test(x:Any) = {
x match {
case m:StringMap => ...
...
}
Run Code Online (Sandbox Code Playgroud)
这是一个错误.它在2.11中修复:
scala> type StringMap = Map[String, Any]
defined type alias StringMap
scala> (Map(4 -> true): Any) match {
| case m: StringMap => true
| case _ => false
| }
<console>:10: warning: non-variable type argument String in type pattern scala.collection.immutable.Map[String,Any] (the underlying of StringMap) is unchecked since it is eliminated by erasure
case m: StringMap => true
^
res0: Boolean = true
Run Code Online (Sandbox Code Playgroud)
它不起作用的原因是由于擦除,你无法分辨出类型参数是什么.如果你想确定它是一个Map[String, Any]而不是其他类型的地图,你必须检查每个键并确保它是一个String.
case m: Map[_,_] if m.keySet.forall(_.isInstanceOf[String]) =>
m.asInstanceOf[Map[String,Any]]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2876 次 |
| 最近记录: |