Skip to content

Instantly share code, notes, and snippets.

@joprice
Created December 19, 2015 04:14
Show Gist options
  • Save joprice/c0a598d1406371f2a218 to your computer and use it in GitHub Desktop.
Save joprice/c0a598d1406371f2a218 to your computer and use it in GitHub Desktop.
Machinist before/after bytecode
/**
Before:
0: getstatic #19 // Field test/Eq$.MODULE$:Ltest/Eq$;
3: iload_1
4: invokestatic #25 // Method scala/runtime/BoxesRunTime.boxToInteger:(I)Ljava/lang/Integer;
7: getstatic #30 // Field test/Eq$intEq$.MODULE$:Ltest/Eq$intEq$;
10: invokevirtual #34 // Method test/Eq$.EqOps:(Ljava/lang/Object;Ltest/Eq;)Ltest/Eq$EqOps;
13: iload_2
14: invokestatic #25 // Method scala/runtime/BoxesRunTime.boxToInteger:(I)Ljava/lang/Integer;
17: invokevirtual #40 // Method test/Eq$EqOps.$eq$eq$eq:(Ljava/lang/Object;)Z
20: ireturn
After:
0: getstatic #19 // Field test/Eq$intEq$.MODULE$:Ltest/Eq$intEq$;
3: iload_1
4: iload_2
5: invokevirtual #22 // Method test/Eq$intEq$.eqv$mcI$sp:(II)Z
8: ireturn
*/
package test
import scala.language.experimental.macros
import scala.{specialized => sp}
import machinist.DefaultOps
trait Eq[@sp(Int, Double) A] {
def eqv(lhs: A, rhs: A): Boolean
}
object Eq {
implicit object intEq extends Eq[Int] {
def eqv(lhs: Int, rhs: Int): Boolean = lhs == rhs
}
implicit class EqOps[A: Eq](x: A) {
//def ===(rhs: A): Boolean = implicitly[Eq[A]].eqv(x, rhs)
def ===(rhs: A): Boolean = macro DefaultOps.binop[A, Boolean]
}
}
object Test {
import Eq.EqOps
def test(a: Int, b: Int) = {
(a === b)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment