I was recently proposed the following puzzle and I failed to solve it. I tried finding a similar problem in https://github.com/scala/bug to no avail. This precise variation doesn’t seem to have been reported or my understanding of the problem is lacking. Hopefully you can enlighten me
Given the following java type:
public class A<T extends A<T>> {}
and the following java interface
public interface IA {
void foo(A a);
}
How can I define an implementation in scala ?
class IAImpl extends IA{
override def foo(a: A[_]): Unit = ???
}
fails to compile with
class IAImpl needs to be abstract, since method foo in trait IA of type (a: A)Unit is not defined
(Note that A does not match A[_]. To implement a raw type, use A[_])
class IAImpl extends IA{
^
trying to include the type bound in the signature :
class IAImpl extends IA{
override def foo(a: A[_ <: A[_]]): Unit = ???
}
fails with
class IAImpl needs to be abstract, since method foo in trait IA of type (a: A)Unit is not defined
(Note that A does not match A[_ <: A[_]]. To implement a raw type, use A[_])
class IAImpl extends IA{
^
any idea how to make this work apart from changing the definition of the interface or creating a bridge implementation in java ?
The actual use case is implementing a WaitStrategy as defined by testcontainer
First you should kick the person that’s still using raw types in Java. Then you might consider filing a bug report, cause that doesn’t look right to me. Or wait until someone more knowledgeable comes in to tell you that this is not a bug for some strange reason.
At the very least, the error message that says that the raw type A can be
implemented by A[_] is a compiler bug. This only works for parameters
without bounds.
I can’t figure out how to override this method even in Java without using
another raw type.
In order to convey the bound, the method needs a parameter, but I can’t
figure out how to do this and still override the original method, neither
in Java nor in Scala.