I'm working on a project and seem to have found a bug in the Scala compiler that makes it impossible to implement certain interfaces. Anybody have any thoughts on what to do about this short of dropping into Java? I am just trying to implement an interface from a third-party library. I have a reduced case below, but this is an actual issue I'm running into in a real project that needs to deal with less-than-perfect APIs.
Suppose that you have the following Java class:
public interface Cmp extends Comparable { }
Although this may generate a "bare generics" warning in Java 5 and above, this is valid code for every version of Java since Comparable was added in 1.2. However, I run into endless troubles when I try to implement Cmp (or anything that extends Cmp) in Scala.
This should be very straightforward.
scala> new Cmp { def compareTo(x: AnyRef): Int = 0 }
<console>:8: error: object creation impossible, since method compareTo in trait Comparable of type (x$1: T)Int is not defined
(Note that T does not match AnyRef)
Hmm, maybe if I match the Java signature exactly:
scala> new Cmp { def compareTo(x: java.lang.Object): Int = 0 }
<console>:8: error: object creation impossible, since method compareTo in trait Comparable of type (x$1: T)Int is not defined
(Note that T does not match Object)
That doesn't work, and Any doesn't work either, so let's try to give it wants even though it makes absolutely no sense:
scala> new Cmp { def compareTo(x: T): Int = 0 }
<console>:8: error: not found: type T
new Cmp { def compareTo(x: T): Int = 0 }
Maybe I can override the missing type?
scala> new Comparable[AnyRef] with Cmp { def compareTo(x: AnyRef): Int = 0 }
<console>:8: error: illegal inheritance;
anonymous class $anon inherits different type instances of trait Comparable:
Comparable[T] and Comparable[AnyRef]
new Comparable[AnyRef] with Cmp { def compareTo(x: AnyRef): Int = 0 }
scala> new Comparable[_] with Cmp { def compareTo(x: AnyRef): Int = 0 }
<console>:8: error: class type required but Comparable[_] found
new Comparable[_] with Cmp { def compareTo(x: AnyRef): Int = 0 }
It seems pretty clear that this is a bug, and I will open one. That said, does anybody have any other suggestions on how to work around this? Short of building a modified version of that interface just for compile time, my only other thought is that maybe I can use dynamic methods, which would be pretty silly.
Thanks! Sarah