1

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

2
  • 2
    Class TMijavatest takes a type parameter, but you are instantiating it without a type argument, so Nothing is inferred. Specify the type argument. For example: val foo = new TMijavatest[String]() Commented Aug 19, 2015 at 11:00
  • 1
    The same holds true for the JavaTest class. It is a generic type, thus you should instantiate it with a type, e.g. val javaTest = new JavaTest[String]. Commented Aug 19, 2015 at 11:46

1 Answer 1

1

You should instantiate JavaTest & TMijavatest with type

val javaTest = new JavaTest[String]

class TMijavatest[T] extends IJavaTest[T] {
  override def ss(): String = ""
}

val foo = new TMijavatest[String]

javaTest.add(foo)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.