Internals of implicit value classes

32 views
Skip to first unread message

Jasper-M

unread,
Aug 26, 2016, 9:03:42 AM8/26/16
to scala-user
Hi,

I was trying to understand more about how implicit value classes allow extension methods without boxing, so I tried looking into the bytecode that got generated.
Now there are a few lines there (in bold) of which I don't really understand the purpose. I'm guessing the empty class names have something to do with this being a REPL session, but I still don't understand why `2` has to be passed to some method before the actual extension method is called.


scala> implicit class IntExt(private val i: Int) extends AnyVal {
     |   def ext(o: Int) = i - o
     | }
defined class IntExt

scala> class Foo{ def foo(i: Int) = 2 ext i }
defined class Foo

scala> :javap Foo

  public int foo(int);
    descriptor: (I)I
    flags: ACC_PUBLIC
    Code:
      stack=3, locals=2, args_size=2
         0: getstatic     #13                 // Field IntExt$.MODULE$:LIntExt$;
         3: getstatic     #18                 // Field .MODULE$:L;
         6: iconst_2
         7: invokevirtual #21                 // Method .IntExt:(I)I
        10: iload_1
        11: invokevirtual #25                 // Method IntExt$.ext$extension:(II)I
        14: ireturn

Also, is it necessary that equals$extension and hashCode$extension methods get generated in IntExt$?


Best,

Jasper
Reply all
Reply to author
Forward
0 new messages