当类层次结构中存在原始类型时,为什么 Scala 会抱怨非法继承?

发布于 2024-10-17 07:23:54 字数 1167 浏览 3 评论 0原文

我正在编写一个采用 Scala 的包装器ObservableBuffer 并触发与 Eclipse/JFace 数据绑定 框架。

在数据绑定框架中,有一个抽象的 ObservableList 装饰普通的 Java 列表。我想重用这个基类,但即使这个简单的代码也失败了:

val list = new java.util.ArrayList[Int]
val obsList = new ObservableList(list, null) {}

有错误:

illegal inheritance; anonymous class $anon inherits different type instances of trait Collection: java.util.Collection[E] and java.util.Collection[E]
illegal inheritance; anonymous class $anon inherits different type instances of trait Iterable: java.lang.Iterable[E] and java.lang.Iterable[E]

为什么?它与原始类型有关吗? ObservableList 实现 IObservableList,它扩展了原始类型 java.util.List。这是预期的行为吗?我该如何解决它?

I'm writing a wrapper that takes a Scala ObservableBuffer and fires events compatible with the Eclipse/JFace Databinding framework.

In the Databinding framework, there is an abstract ObservableList that decorates a normal Java list. I wanted to reuse this base class, but even this simple code fails:

val list = new java.util.ArrayList[Int]
val obsList = new ObservableList(list, null) {}

with errors:

illegal inheritance; anonymous class $anon inherits different type instances of trait Collection: java.util.Collection[E] and java.util.Collection[E]
illegal inheritance; anonymous class $anon inherits different type instances of trait Iterable: java.lang.Iterable[E] and java.lang.Iterable[E]

Why? Does it have to do with raw types? ObservableList implements IObservableList, which extends the raw type java.util.List. Is this expected behavior, and how can I work around it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

习ぎ惯性依靠 2024-10-24 07:23:54

继承层次结构中存在 Java 原始类型会导致此类问题。一种解决方案是编写一点 Java 来修复原始类型,如 Scala 类无法覆盖扩展 java.util.comparator 的 Java 接口中的比较方法

有关为什么原始类型对于 scala 存在问题的更多信息,请参阅此错误 http://lampsvn.epfl.ch/trac/scala/ticket/1737 。该错误有一个使用存在类型的解决方法,该方法可能不适用于这种特殊情况,至少在没有大量转换的情况下是这样,因为 java.util.List 类型参数同时位于 co 和 contra 变体位置。

Having a Java raw type in the inheritance hierarchy causes this kind of problem. One solution is to write a tiny bit of Java to fix up the raw type as in the answer for Scala class cant override compare method from Java Interface which extends java.util.comparator

For more about why raw types are problematic for scala see this bug http://lampsvn.epfl.ch/trac/scala/ticket/1737 . That bug has a workaround using existential types that probably won't work for this particular case, at least not without a lot of casting, because the java.util.List type parameter is in both co and contra variant positions.

一念一轮回 2024-10-24 07:23:54

从 javadoc 来看,构造函数的参数没有参数化。

我会尝试这个:

val list = new java.util.ArrayList[_]
val obsList = new ObservableList(list, null) {}

From looking at the javadoc the argument of the constructor isn't parameterized.

I'd try this:

val list = new java.util.ArrayList[_]
val obsList = new ObservableList(list, null) {}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文