使用scala调用java.util.Hashtable#put
我在调用老式哈希表时遇到了意外的麻烦。这是怎么回事?
Welcome to Scala version 2.8.0.final (Java HotSpot(TM) Client VM, Java 1.6.0_21).
Type in expressions to have them evaluated.
Type :help for more information.
scala> import com.ibm.mq._
import com.ibm.mq._
scala> MQEnvironment.properties
res1: java.util.Hashtable[_, _] = {}
scala> res1.put("transport", "MQSeries")
<console>:10: error: type mismatch;
found : java.lang.String("transport")
required: ?0 where type ?0
res1.put("transport", "MQSeries")
^
PS,问题仍然存在,但我有一个令人讨厌的解决方法:
scala> new java.util.Hashtable[String, String]
res10: java.util.Hashtable[String,String] = {}
scala> res10.put("transport", "MQSeries")
res11: String = null
scala> MQEnvironment.properties = res10
scala> MQEnvironment.properties
res13: java.util.Hashtable[_, _] = {transport=MQSeries}
I've an unexpected trouble calling put on an old-school hashtable. What's going on here?
Welcome to Scala version 2.8.0.final (Java HotSpot(TM) Client VM, Java 1.6.0_21).
Type in expressions to have them evaluated.
Type :help for more information.
scala> import com.ibm.mq._
import com.ibm.mq._
scala> MQEnvironment.properties
res1: java.util.Hashtable[_, _] = {}
scala> res1.put("transport", "MQSeries")
<console>:10: error: type mismatch;
found : java.lang.String("transport")
required: ?0 where type ?0
res1.put("transport", "MQSeries")
^
PS, the question still stands as is, but I have a nasty workaround:
scala> new java.util.Hashtable[String, String]
res10: java.util.Hashtable[String,String] = {}
scala> res10.put("transport", "MQSeries")
res11: String = null
scala> MQEnvironment.properties = res10
scala> MQEnvironment.properties
res13: java.util.Hashtable[_, _] = {transport=MQSeries}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
properties
接口似乎是早于 Java 泛型的老式 API 之一。java.util.HashTable[_, _]
中的下划线是存在类型的简写,其中第一个(键类型)对应于出现在诊断。这些旧的 Java“原始”类型是 Scala 的 Java 互操作性中的一个不幸的、可见的接缝,尽管这种接缝通常只出现在非常旧的 API 中。That
properties
interface appears to be one of those old-school APIs that pre-date Java generics. Those underscores injava.util.HashTable[_, _]
are shorthands for existential types, the first of which (the key type) corresponds to the?0
appearing in the diagnostic. These old Java "raw" types are an unfortunate, visible seam in Scala's Java interoperability, albeit one that usually shows up only in very old APIs.