In this code:
class Foo[T] { def bar(i: T) = i } object Main { def main(args: Array[String]) { val f = new Foo[Int] f.bar(5) } }
The bar call should first insert an integer. Compilation using Scala 2.8.1 and using:
javap -c -l -private -verbose -classpath <dir> Main$
to see that the bytecode created for the main method of the main class gives:
public void main(java.lang.String[]); ... 9: iconst_5 10: invokestatic #24;
You can call BoxesRunTime before calling bar .
BoxesRunTime is an object that contains boxing methods for primitive types, so there must be exactly one instance as a whole. The trick here is that this particular file in the library was written in Java, and conversions are static methods. For this reason, there are no instances at runtime, although using it in Scala code looks as if it were an object.
You should probably look for nested primitives (like java.lang.Integer) with JProfile, although I'm not sure how the JVM works, and whether it can really rewrite the code at runtime and optimize it to avoid boxing. As far as I know, he should not apply specialization (but I believe that the CLR). A few micro-businesses with and without boxing are another way to find out what happens at runtime.
EDIT:
The above assumes that the type parameter was not annotated with the @specialized annotation. In this case, boxing / unpacking can be avoided. Certain classes in the standard library are specialized. See this file .
axel22
source share