Currently, the following code:
Is transformed to:
val x: Int[] = Array.apply(1, Predef.wrapIntArray([2,3]))
Which is as inefficient as it looks. scalac has rewriting rules in Cleanup to deal with this: https://github.com/scala/scala/blob/d030172d7ef807d85391d32c5456b1b97b15a402/src/compiler/scala/tools/nsc/transform/CleanUp.scala#L536-L545
We could do something similar, keeping in mind the following:
- We have to be careful to not break semantics, see SI-6611
- We'll need more (or better) rewriting rules than
scalac to deal with all possible cases, for example scalac optimizes Array(1,2,3) and Array[String]("foo", "bar") but does nothing with Array[Int](1,2,3).
- I'd like it if the rewriting was done before
Erasure, so that it does not need to take into account whatever crazy encoding we'll come up with for arrays of value classes.
The alternative would be to do this and other optimizations like List(1,2,3) using macros, see https://groups.google.com/forum/#!topic/scala-internals/5oRYBYBUqMY/discussion and SI-6247, is this the direction we want to go in?