From scala I need to create a java object than it has a method that it demands a parameter that it is a generic interface
java class
public class JavaTest<T> {
public List<T> add(IJavaTest<T> javaTest){
return null;
}
}
java interface
public interface IJavaTest<T> {
String ss();
}
and in scala I implement this:
val javaTest = new JavaTest
class TMijavatest[T] extends IJavaTest[T] {
override def ss(): String = ""
}
val foo=new TMijavatest()
But when I call the add method
javaTest.add(foo);
I have this error in intellij: Type mismatch, expected:IJavaTest[T], actual TMijavatest[Nothing]
with:
javaTest.add(foo.asInstanceOf);
It compiles me but in execution time I have this error:
Unexpected error occured in the server. Caused by: java.lang.ClassCastException: my.pakage.Test$TMijavatest$1 cannot be cast to scala.runtime.Nothing$
if I generate the foo object just like this:
val foo = new TMijavatest[String]
javaTest.add(foo);
I have this error in intellij: Type mismatch, expected:IJavaTest[T], actual TMijavatest[String]
How I can do this? Thank you
TMijavatesttakes a type parameter, but you are instantiating it without a type argument, soNothingis inferred. Specify the type argument. For example:val foo = new TMijavatest[String]()JavaTestclass. It is a generic type, thus you should instantiate it with a type, e.g.val javaTest = new JavaTest[String].