Scala Java 深度映射转换(ConcurrentHashMap)
我有一个 ConcurrentHashMap 对象定义如下:
import scala.collection.JavaConversions._
val storage: ConcurrentMap[String,ConcurrentMap[Int,ConcurrentMap[String,Double]]] =
new ConcurrentHashMap[String,ConcurrentHashMap[Int,ConcurrentHashMap[String,Double]]]
Scala (2.8.1) 在编译时抱怨以下错误:
found : java.util.concurrent.ConcurrentHashMap[String,java.util.concurrent.ConcurrentHashMap[String,String]]
required: scala.collection.mutable.ConcurrentMap[String,scala.collection.mutable.ConcurrentMap[String,String]]
但是当我尝试以下代码时,它起作用了:
val storage: ConcurrentMap[String,Double] = new ConcurrentHashMap[String,Double]
我感谢您对如何修复此错误的评论。
I have a ConcurrentHashMap object defined as below:
import scala.collection.JavaConversions._
val storage: ConcurrentMap[String,ConcurrentMap[Int,ConcurrentMap[String,Double]]] =
new ConcurrentHashMap[String,ConcurrentHashMap[Int,ConcurrentHashMap[String,Double]]]
And Scala (2.8.1) complains at compilation with the following error:
found : java.util.concurrent.ConcurrentHashMap[String,java.util.concurrent.ConcurrentHashMap[String,String]]
required: scala.collection.mutable.ConcurrentMap[String,scala.collection.mutable.ConcurrentMap[String,String]]
But when I try the following code, it works:
val storage: ConcurrentMap[String,Double] = new ConcurrentHashMap[String,Double]
I appreciate your comment how to fix this error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
隐式转换(例如 JavaConversions 提供的转换)在实例级别而不是类型级别工作。因此,虽然最外层的地图可以转换,但内部的地图则不能。
没有办法直接隐式转换嵌套的情况。而且,不幸的是,在以下示例中似乎没有正确选择最具体的隐式:
因此,在最终分析中,我预测将来会出现很多类型注释(上面的 for 理解可能会与显式类型归属一起使用,但我太困了,无法把所有内容都打出来。)
Implicit conversions, such as the one provided by JavaConversions, work at the instance level rather than the type level. So, while the outermost map can be converted, the inner maps will not be.
There isn't a way to directly have the nested case implicitly converted. And, unfortunately, it appears that the most specific implicit is not being correctly selected in the following examples:
So, in final analysis, I predict a lot of type annotations in your future (the for comprehensions above would probably work with explicit type ascription, but I'm too sleepy to type it all out.)