Monday, May 5, 2014

Type Erasure in Scala and Java

  • Martin Odersky tried to introduce generics to Java without modifying the JVM by translating away generics source-to-source. He and others reworked a Java compiler based on this technique; their works eventually evolved to JDK 1.5.
  • Generics in Scala and JDK 1.5 (and later) has to use type erasure, because JVM does not understand generic types―specification of Java bytecodes does not allow defining parameterized type of variables. Any modification to the bytecode to support reified generics would break backwards compatibility.
  • While it is possible for Scala to compile to bytecodes that support reified generics, this comes at the cost of giving up interoperability with Java classes and this is the last thing the designers of Scala would do. 
  • On the other hand, C# supposes generics from 2.0. It does not have to use erasure because .NET started to accommodate generics since then. 

References

1. http://lampwww.epfl.ch/~emir/bqbase/2006/10/16/erasure.html
2. http://code.stephenmorley.org/articles/java-generics-type-erasure/

No comments:

Post a Comment