1

I'm trying to implement a generic java interface in scala. I have looked at: How do I extend Java interface containing generic methods in Scala? And Scala: Overriding Generic Java Methods II

But still I could not find an answer. Here's the method signature from Spring web:

T read(Class<? extends T> clazz, HttpInputMessage inputMessage)
            throws IOException, HttpMessageNotReadableException;

I tried the following in scala:

 @throws(classOf[IOException])
  @throws(classOf[HttpMessageNotReadableException])
  override def read[T](clazz : Class[_ <: T], inputMessage : HttpInputMessage) : T ={
  } 

But I get an error saying that this method overrides nothing. If I erase the type by doing:

override def read(clazz : Class[_], inputMessage : HttpInputMessage) : AnyRef ={

It marks the method as being overwritten. My question is how can I keep type safety here and force it to override the interface method?

Regards

EDIT

The spring interface:

public interface HttpMessageConverter<T> {

T read(Class<? extends T> clazz,
       HttpInputMessage inputMessage)
       throws IOException,
              HttpMessageNotReadableException
}
1
  • 1
    Could you provide a link for the source code of the Spring Web where the method you are trying to override/implement resides? Commented Mar 25, 2012 at 18:27

2 Answers 2

8

I think the problem is likely to be that you have added a type parameter to the read method, rather than using the type parameter from your class declaration:

class MyImpl[T] extends JavaInterface[T] {

  override def read(clazz: Class[_ <: T], ... )

}    

If we rename your T to U it becomes clearer what you have done:

class MyImpl[T] extends JavaInterface[T] {

  /** U is not T */
  override def read[U](clazz: Class[_ <: U], ... )

}    

You might also try and sing "U is not T" to the tune of REM's Losing My Religion to hammer the point home.

Sign up to request clarification or add additional context in comments.

Comments

0

In java you have parameterilized interface, but in scala you try to parameterize method.

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.