Reputation: 4156
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
}
Upvotes: 1
Views: 2397
Reputation: 134330
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.
Upvotes: 8
Reputation: 6930
In java you have parameterilized interface, but in scala you try to parameterize method.
Upvotes: 0