Skip to content

Instantly share code, notes, and snippets.

@non
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save non/aacc4a86ec5971faea7c to your computer and use it in GitHub Desktop.
Save non/aacc4a86ec5971faea7c to your computer and use it in GitHub Desktop.

test.scala

package test

case class UByte(val byte: Byte) extends AnyVal {
  def ==(rhs: UByte): Boolean = byte == rhs.byte
}

class Test {
  def test1(x: UByte, y: UByte): Boolean = x == y
  def test2(x: UByte, y: Any): Boolean = x == y
  def test3(x: Any, y: UByte): Boolean = x == y
  def test4(x: Any, y: Any): Boolean = x == y
}

compile and decompile

$ scalac test.scala
$ javap -c test.Test

test1 bytecode

  public boolean test1(byte, byte);
    Code:
       0: getstatic     #16                 // Field test/UByte$.MODULE$:Ltest/UByte$;
       3: iload_1
       4: iload_2
       5: invokevirtual #19                 // Method test/UByte$.$eq$eq$extension:(BB)Z
       8: ireturn

test2 bytecode

  public boolean test2(byte, java.lang.Object);
    Code:
       0: new           #28                 // class test/UByte
       3: dup
       4: iload_1
       5: invokespecial #32                 // Method test/UByte."<init>":(B)V
       8: aload_2
       9: astore_3
      10: dup
      11: ifnonnull     22
      14: pop
      15: aload_3
      16: ifnull        29
      19: goto          33
      22: aload_3
      23: invokevirtual #36                 // Method java/lang/Object.equals:(Ljava/lang/Object;)Z
      26: ifeq          33
      29: iconst_1
      30: goto          34
      33: iconst_0
      34: ireturn

test3 bytecode

  public boolean test3(java.lang.Object, byte);
    Code:
       0: aload_1
       1: new           #28                 // class test/UByte
       4: dup
       5: iload_2
       6: invokespecial #32                 // Method test/UByte."<init>":(B)V
       9: astore_3
      10: dup
      11: ifnonnull     22
      14: pop
      15: aload_3
      16: ifnull        29
      19: goto          33
      22: aload_3
      23: invokevirtual #36                 // Method java/lang/Object.equals:(Ljava/lang/Object;)Z
      26: ifeq          33
      29: iconst_1
      30: goto          34
      33: iconst_0
      34: ireturn

test4 bytecode

  public boolean test4(java.lang.Object, java.lang.Object);
    Code:
       0: aload_1
       1: aload_2
       2: invokestatic  #45                 // Method scala/runtime/BoxesRunTime.equals:(Ljava/lang/Object;Ljava/lang/Object;)Z
       5: ifeq          12
       8: iconst_1
       9: goto          13
      12: iconst_0
      13: ireturn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment