Scala and Type Erasure

27 views
Skip to first unread message

Mahmood Ali

unread,
May 29, 2011, 5:53:41 PM5/29/11
to scala-user
Greetings,

I think I identified a potential bug[1] in the Scala compiler. The
compiler doesn't seem to calculate the type erasure of Java's
intersection types properly. I wanted to confirm that this is indeed
a bug (and an unreported bug), before filing it. Please check the
Stackoverflow thread[1] for instructions to reproduce the problem.

The issue seems to be that the rules of erasure of intersection types
of Scala and Java are different. The type erasure of a type variable
with an intersection type in Java is simply the type erasure of the
first type in its bound[2], while in Scala the type erasure is the
type erasure of intersection dominator which is the first class
(not-trait) that is not shadowed by another subclass within any of the
bounds[3].

So for a Java method A in

class A { <T extends Object & CharSequence> T m() { return null; } }

Java would refer to the method with the following signature
`A.m:()Ljava/lang/Object;` as Object is the first bound, while Scala
would refer to it as `A.m()Ljava/lang/CharSequence;` since Object is
shadowed by CharSequence.

Regards,
Mahmood

[1] http://stackoverflow.com/questions/6168756/nosuchmethoderror-when-calling-java-code-from-scala/6169581#6169581
[2] http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.4
[3] https://lampsvn.epfl.ch/trac/scala/browser/scala/tags/R_2_9_0_1/src//compiler/scala/tools/nsc/transform/Erasure.scala#L109

Reply all
Reply to author
Forward
0 new messages