Does Scala specialize?

I got the impression that the specialization is taking shape, but in the following example it does not look like this:

trait Key [@specialized(Int) A] { 
  def up(k: A): Unit
}

class Test[@specialized(Int) A](key: Key[A], value: A) { 
  key.up(value)
}

If I dump this in a Scala REPL with :javap -v Test, it seems that the call key.upstill puts the primitive:

public Test(Key, java.lang.Object);
  Code:
   Stack=2, Locals=3, Args_size=3
   0:   aload_0
   1:   aload_1
   2:   putfield    #17; //Field key:LKey;
   5:   aload_0
   6:   aload_2
   7:   putfield    #19; //Field value:Ljava/lang/Object;
   10:  aload_0
   11:  invokespecial   #24; //Method java/lang/Object."<init>":()V
   14:  aload_0
   15:  getfield    #17; //Field key:LKey;
   18:  aload_0
   19:  getfield    #19; //Field value:Ljava/lang/Object;
   22:  invokeinterface #30,  2; //InterfaceMethod Key.up:(Ljava/lang/Object;)V
   27:  return

So, is specialization completely useless for developing common implementations? In my case, data structures would completely undermine efforts to develop concise modular code. Therefore, I hope that I have something missing ...

+5
source share
1 answer

You are looking at a non-specialized version of a class.

, Test$mcI$sp, . , , .

: REPL, . REPL , , println, :

scala> println(new Test(1).getClass.getName)
$line1.$read$$iw$$iw$Test$mcI$sp

scala> :javap $line1.$read$$iw$$iw$Test$mcI$sp
+5

All Articles