2

Scala compiler behaves weirdly with boxing/unboxing in tuples as parameters.

Consider the code:

scala> class Test { def test(p: (Int, String)) = println(p) }
defined class Test

scala> classOf[Test].getMethods()(0)
res2: java.lang.reflect.Method = public void Test.test(scala.Tuple2)

scala> classOf[Test].getMethods()(0).getGenericParameterTypes
res3: Array[java.lang.reflect.Type] = Array(scala.Tuple2<java.lang.Object, java.lang.String>)

scala> // WTF??????                                      ^^^^^^^^^^^^^^^^

Thus, I'm getting Object instead of Integer. I assume this is somehow related to tuple parameter being @specialized, but cannot wrap my head around how to avoid/fix this.

The problem it causes - it is impossible to reconstruct method parameter via reflection on method signature (e.g. while parsing json).

Even if there's a way to get the right type with scala-reflect it doesn't help much, cause there are a lot of Java libraries around (like Jersey) that use just Java reflection.

UPD: OK, putting an Integer (instead of Int) into Tuple works ok. But why isn't it done automatically?

tuxSlayer
  • 2,804
  • 2
  • 20
  • 24
  • What is your question? – Michael Zajac Mar 11 '17 at 00:55
  • @MichaelZajac, why compiler generates wrong code and how to fix/workaround this? – tuxSlayer Mar 11 '17 at 01:00
  • http://stackoverflow.com/questions/20749536/why-dont-scala-primitives-show-up-as-type-parameters-in-java-reflection – Michael Zajac Mar 11 '17 at 01:08
  • See this [Differance between Int and Integer](http://stackoverflow.com/questions/1269170/what-is-are-differences-between-int-and-integer-in-scala) – Jonatan Mar 11 '17 at 01:14
  • Well, while I understand how boxing works, I still didn't get why to generate code with Object instead of the correct type :) – tuxSlayer Mar 11 '17 at 01:17
  • BTW, @Jonatan, it's not the case anymore. Comparison now works well with Integer in scala. So the difference is that Int is a facade for Primitive (int) or Integer (which extends Object). – tuxSlayer Mar 11 '17 at 01:20

0 Answers0