Scala cannot override the method whose parameter is raw type of a scala defined class

for example,define a scala class :

class scalaclass[T] {
val i: Array[T] = null
}

define a java interface whose method raw use this class :

public interface cc {
public scalaclass apply();
}

finally, implement this interface in scala:

class typetest extends cc {
override def apply(): scalaclass[_] = {null}
}

then compiler report error :

“overriding method apply in trait cc of type ()scalaclass;
method apply has incompatible type
override def apply(): scalaclass[_] = {”

i tried scalaclass [ _ ], scalaclass[Any], scalaclass[AnyRef], scalaclass… all dont work.
could someone help me?

I don’t think this is possible - there simply is no equivalent to the Java raw type on the Scala side. (And I think that’s a good thing.) Perhaps this blog post can give you some inspiration to tackle things from a slightly different angle on the Java side.

1 Like