Created
July 19, 2012 18:09
-
-
Save JoshRosen/3145737 to your computer and use it in GitHub Desktop.
Wildcard upper type bounds in Scala and Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
JavaTest.java:11: g(java.lang.Class<? extends java.lang.Iterable<?>>) in ScalaTest cannot be applied to (java.lang.Class<java.util.ArrayList>) | |
ScalaTest.g(ArrayList.class); | |
^ | |
JavaTest.java:15: <T>f(java.lang.Class<T>) in ScalaTest cannot be applied to (java.lang.Class<java.lang.Object>) | |
ScalaTest.f(Object.class); | |
^ | |
JavaTest.java:16: g(java.lang.Class<? extends java.lang.Iterable<?>>) in ScalaTest cannot be applied to (java.lang.Class<java.lang.Object>) | |
ScalaTest.g(Object.class); | |
^ | |
JavaTest.java:17: <T>f(java.lang.Class<T>) in JavaTest cannot be applied to (java.lang.Class<java.lang.Object>) | |
JavaTest.f(Object.class); | |
^ | |
JavaTest.java:18: g(java.lang.Class<? extends java.lang.Iterable>) in JavaTest cannot be applied to (java.lang.Class<java.lang.Object>) | |
JavaTest.g(Object.class); | |
^ | |
5 errors |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.lang.Iterable; | |
public class JavaTest { | |
public static <T extends Iterable> void f(Class<T> x) {} | |
public static void g(Class<? extends Iterable> x) {} | |
public static void main(String[] args) { | |
// Should compile: | |
ScalaTest.f(ArrayList.class); | |
ScalaTest.g(ArrayList.class); | |
JavaTest.f(ArrayList.class); | |
JavaTest.g(ArrayList.class); | |
// Should fail: | |
ScalaTest.f(Object.class); | |
ScalaTest.g(Object.class); | |
JavaTest.f(Object.class); | |
JavaTest.g(Object.class); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.Iterable | |
object ScalaTest { | |
def f[T <: Iterable[_]](x : Class[T]) {} | |
def g(x : Class[_ <: Iterable[_]]) {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The Java type
Class<? extends Iterable>
is not equivalent toClass<? extends Iterable<?>>
. The Scala typeClass[_ <: Iterable[_]
appears to be equivalent toClass<? extends Iterable<?>>
, but the Java typeClass<? extends Iterable>
appears to have no direct Scala equivalent.The same compilation error occurs if the Java method
JavaTest.g()
is redefined asso this is not a Scala issue.