16

In Scala,

{ x: Option[Int] => x }
   .getClass
   .getMethod("apply", classOf[Option[_]])
   .getGenericParameterTypes

returns Array(scala.Option<java.lang.Object>). I'd initially been expecting to see instead Array(scala.Option<scala.Int>), but I see that scala.Int is a value class (extends AnyVal) 'whose instances are not represented as objects by the underlying host system'.

I still don't understand the erasure to Object, though. Couldn't it be the much more useful java.lang.Integer?

Seth Tisue
  • 29,985
  • 11
  • 82
  • 149
Scott Morrison
  • 3,100
  • 24
  • 39
  • 1
    That isn't Scala doing that -- it's Java. To get useful, as in non-Object, results from "GenericParameterTypes" (a method of a Java Class object), a Subclass has to be created. Scala *didn't* create a Subclass, say `OptionInt`, for `Option[Int]`. (See @specialized, perhaps?) –  Jun 23 '12 at 06:34
  • 2
    @pst; I think you're on the wrong track. Change `Int` to `Symbol` above and you get `Array(scala.Option)`. – Scott Morrison Jun 23 '12 at 07:03
  • Hmm, well there goes my hypothesis :-/ I blame compiler magic. –  Jun 23 '12 at 07:36

1 Answers1

12

Couldn't it be the much more useful java.lang.Integer?

Yes, and that was even the case, once. Unfortunately, that leads to broken type signatures. That is, it is impossible to generate correct bytecode in all situations if Int is erased to java.lang.Integer.

There isn't a single ticket or commit about this, but the one that changed this particular behavior is scala/bug#4214, in this commit.

Seth Tisue
  • 29,985
  • 11
  • 82
  • 149
Daniel C. Sobral
  • 295,120
  • 86
  • 501
  • 681