当类层次结构中存在原始类型时,为什么Scala会抱怨非法继承? [英] Why does Scala complain about illegal inheritance when there are raw types in the class hierarchy?
问题描述
我正在编写一个包含Scala的包装器 ObservableBuffer
并触发与 Eclipse / JFace数据绑定框架。
I'm writing a wrapper that takes a Scala ObservableBuffer
and fires events compatible with the Eclipse/JFace Databinding framework.
在数据绑定框架中,有一个抽象 <$ c装饰普通Java列表的$ c> ObservableList 。我想重用这个基类,但即使这个简单的代码也会失败:
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) {}
有错误:
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
。这是预期的行为,我该如何解决它?
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?
推荐答案
在继承层次结构中使用Java原始类型会导致这种情况问题。一种解决方案是编写一小部分Java来修复原始类型,如 Scala类无法覆盖扩展java.util.comparator的Java接口的比较方法
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
有关原因类型为scala存在问题的原因,请参阅 http://lampsvn.epfl .ch / trac / scala / ticket / 1737 。该bug有一个解决方法,使用可能不适用于这种特定情况的存在类型,至少在没有大量转换的情况下,因为java.util.List类型参数在co和contra变体位置。
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.
这篇关于当类层次结构中存在原始类型时,为什么Scala会抱怨非法继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!