Why does Scala complain about illegal inheritance when there are raw types in the class hierarchy?
Asked Answered
A

2

4

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?

Aime answered 14/2, 2011 at 11:35 Comment(7)
I don't know the answer, but does it work when the list is parameterized by an object instead of a primitive? (E.g. ArrayList[String] or ArrayList[java.lang.Integer]?)Volans
No, I still get the same errors.Aime
I'm no expert in Scala, but why are you using the curly braces in the initialization of ObservableList? Isn't this some kind of a partial function? In Java that would be inheritance, but why would you want to inherit it?Sneer
ObservableList is abstract but with no abstract methods. I need the curly braces to instantiate an anonymous subclass.Aime
Do you hava a pointer to the javadoc of ObservableList? I googled, but found various different onesLexie
@Jens: I've added in in the question.Aime
In the (temporary?) absence of a way to deal with this problem in Scala, I wrote a custom subclass in Java which I can then use from Scala. Not very nice, but it works.Aime
M
5

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.

Madox answered 15/2, 2011 at 15:23 Comment(0)
L
-2

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) {}
Lexie answered 14/2, 2011 at 14:54 Comment(1)
Something like new ParametrizedType[_] has never worked (neither does new Thing<?> in Java); it doesn't compile.Aime

© 2022 - 2024 — McMap. All rights reserved.