使用scala调用java.util.Hashtable#put

发布于 2024-09-15 20:59:24 字数 997 浏览 3 评论 0原文

我在调用老式哈希表时遇到了意外的麻烦。这是怎么回事?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

君勿笑 2024-09-22 20:59:24

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 in java.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文