New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot Implement Method of Java Interface with Type Parameter Omitted by It's Sub-Class #1737
Comments
Imported From: https://issues.scala-lang.org/browse/SI-1737?orig=1 |
@lrytz said: |
@dcaoyuan said: public class JS extends B { |
@odersky said: The only thing one can do in these situations is implement a subclass in Java that implements the method. I'm afraid there's nothing else we can do without introducing raw types into Scala. And the latter we want to avoid. |
@paulp said: If that sounds plausible to you I'd like to reopen this ticket as an enhancement. |
michid4 said: public interface A<T extends String> {
T get();
}
public abstract class B implements A {}
class C extends B {
this: A[_] =>
def get = "foo"
} See http://michid.wordpress.com/2009/06/24/puzzle-implement-this-solution/ If we could actually refer to the existential type used in the self type, this could probably also be used to work around the original issue. |
@adriaanm said: error: illegal cyclic reference involving type _$$1
this: A[_] =>
^ |
@cunei said: |
Minim case:
There is a Java interface A:
public interface A {
void run(T t);
}
Which has a type parameter and abstract method run(T t)
And a Java abstract class B with is sub-class of A. But, B, as it,
omitted type parameter of A. This is unsafe but valid in Java:
public abstract class B implements A {
public String me() {
return "I'm B";
}
}
Assume above class A and B have been compiled under javac, and packed
as jar library, and I can not patch it anymore. Now I need to write a
class S in Scala which extends B:
class S extends B {
override
def runT <: String = {println(t)}
}
scalac complained as:
/Users/dcaoyuan/NetBeansProjects/ScalaTestCase/src/S.scala:1: error:
class S needs to be abstract, since method run in trait A of type
(T)Unit is not defined
class S extends B {
/Users/dcaoyuan/NetBeansProjects/ScalaTestCase/src/S.scala:3: error:
method run overrides nothing
def runT <: String = {println(t)}
I than tried "forSome" type:
class S extends B {
override
def run(t:T forSome {type T <: String}) = {println(t)}
}
Again, I got:
/Users/dcaoyuan/NetBeansProjects/ScalaTestCase/src/S.scala:1: error:
class S needs to be abstract, since method run in trait A of type
(T)Unit is not defined
class S extends B {
/Users/dcaoyuan/NetBeansProjects/ScalaTestCase/src/S.scala:3: error:
method run overrides nothing
def run(t:T forSome {type T <: String}) = {println(t)}
It seems that, since B omitted A's type parameter T, I have no way to
get what is T, and can not successfully override/implement "run(T t)"
method.
I tried other forms of "forSome" usages, and always failed.
The text was updated successfully, but these errors were encountered: