Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala class cant override compare method from Java Interface which extends java.util.comparator

I'm currently working on a port of a jEdit plugin to write all code in Scala. However Im forced at a certain point to implement my own Comparator.

My simplified code is as follows:

class compare extends MiscUtilities.Compare {
  def compare(obj1: AnyRef, obj2: AnyRef): Int = 1
}

The MiscUtilities.Compare has the following signature when looking from IntelliJ

public static interface Compare extends java.util.Comparator {        
    int compare(java.lang.Object o, java.lang.Object o1);
}

However when im trying to compile my class I get a error saying:

error: class compare needs to be abstract, since method compare in trait Comparator of type (x$1: T,x$2: T)Int is not defined class compare extends MiscUtilities.Compare {

I have also tried with Any and java.lang.Object as types, but no luck so far.

Help is very much appreciated:)

Regards Stefan

like image 405
Stefan Avatar asked Jul 25 '10 11:07

Stefan


3 Answers

It may be impossible. If this is wrong, I would like to know.

EDIT: But you can do this instead:

// CompareImpl.java
abstract class CompareImpl implements MiscUtilities.Compare {
  public int compare(Object o, Object o1) {
    return doCompare(o, o1);
  }

  public abstract int doCompare(Object o, Object o1);
}

// MyCompare.scala
object MyCompare extends CompareImpl {
  def doCompare(a: AnyRef, b: AnyRef) = 0
}

So you still have to write some Java, but only one trivial class for each interface like MiscUtilities.Compare.

like image 105
Alexey Romanov Avatar answered Sep 21 '22 04:09

Alexey Romanov


Producing a standalone example is the first step to solving these sort of problems.

 ~/code/scratch/raw: cat Compare.java 
interface Compare extends java.util.Comparator {
}
 ~/code/scratch/raw: cat MyCompare.scala 
object MyCompare extends Compare  {
  def compare(a: AnyRef, b: AnyRef) = 0
}
 ~/code/scratch/raw: scalac MyCompare.scala Compare.java 
MyCompare.scala:1: error: object creation impossible, since method compare in trait Comparator of type (x$1: T,x$2: T)Int is not defined
object MyCompare extends Compare  {
       ^

The temptation is to extend Comparator[AnyRef] directly from MyCompare, along with Compare:

object MyCompare extends java.util.Comparator[AnyRef] with Compare  {
  def compare(a: AnyRef, b: AnyRef) = 0
}

But this leads to:

error: illegal inheritance;
 self-type MyCompare.type does not conform to java.util.Comparator[AnyRef]'s selftype java.util.Comparator[AnyRef]

So I'm inclined to agree that this isn't possible, at least directly. Write the class in Java, delegating to Scala if need be.

like image 2
retronym Avatar answered Sep 20 '22 04:09

retronym


Does it work if you put [AnyRef] after MiscUtilities.Compare? I.e.,

class Compare extends MiscUtilities.Compare[AnyRef] {   
  def compare(a1:AnyRef, a2:AnyRef) = 0       
}

I tried that with java.util.Comparator directly and the compiler seemed happy.

like image 1
Dean Wampler Avatar answered Sep 21 '22 04:09

Dean Wampler