Skip to content

Instantly share code, notes, and snippets.

@knutwalker
Created January 27, 2016 22:20
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 knutwalker/ed75a2e45f8857e3db5d to your computer and use it in GitHub Desktop.
Save knutwalker/ed75a2e45f8857e3db5d to your computer and use it in GitHub Desktop.
sealed trait Thingy[+A] {
def bla(x: String): String
val bla2: String ⇒ String = bla
}
case class Good[A](x: A) extends Thingy[A] { def bla(x: String) = x }
case class Bad(t: String) extends Thingy[Nothing] { def bla(x: String) = t }
object Thingy {
def good1[A](x: A): Thingy[A] = Good(x)
def good2[A]: A => Thingy[A] = _good.asInstanceOf[A => Thingy[A]]
def bad1(t: String): Thingy[Nothing] = Bad(t)
val bad2: String => Thingy[Nothing] = Bad.apply
private[this] val _good: Any => Thingy[Any] = Good.apply
}
// <name>8 variants are possible 2.12. bytecode, generated with
// -cp ~/.ivy2/cache/org.scala-lang.modules/scala-java8-compat_2.11/bundles/scala-java8-compat_2.11-0.7.0.jar:.
// -target:jvm-1.8
// -Xexperimental
// -Ydelambdafy:method
// -Ybackend:GenBCode
object Blippy {
def main(args: Array[String]): Unit = {
// 0: getstatic #19 // Field Thingy$.MODULE$:LThingy$;
// 3: bipush 42
// 5: invokestatic #25 // Method scala/runtime/BoxesRunTime.boxToInteger:(I)Ljava/lang/Integer;
// 8: invokevirtual #29 // Method Thingy$.good1:(Ljava/lang/Object;)LThingy;
// 11: astore_2
val a = Thingy.good1(42)
// 0: getstatic #26 // Field Thingy$.MODULE$:LThingy$;
// 3: bipush 42
// 5: invokestatic #32 // Method scala/runtime/BoxesRunTime.boxToInteger:(I)Ljava/lang/Integer;
// 8: invokevirtual #36 // Method Thingy$.good1:(Ljava/lang/Object;)LThingy;
// 11: astore_2
val a8 = Thingy.good1(42)
// 12: getstatic #19 // Field Thingy$.MODULE$:LThingy$;
// 15: invokevirtual #33 // Method Thingy$.good2:()Lscala/Function1;
// 18: bipush 42
// 20: invokestatic #25 // Method scala/runtime/BoxesRunTime.boxToInteger:(I)Ljava/lang/Integer;
// 23: invokeinterface #39, 2 // InterfaceMethod scala/Function1.apply:(Ljava/lang/Object;)Ljava/lang/Object;
// 28: checkcast #41 // class Thingy
// 31: astore_3
val b = Thingy.good2(42)
// 12: getstatic #26 // Field Thingy$.MODULE$:LThingy$;
// 15: invokevirtual #40 // Method Thingy$.good2:()Lscala/Function1;
// 18: bipush 42
// 20: invokestatic #32 // Method scala/runtime/BoxesRunTime.boxToInteger:(I)Ljava/lang/Integer;
// 23: invokeinterface #46, 2 // InterfaceMethod scala/Function1.apply:(Ljava/lang/Object;)Ljava/lang/Object;
// 28: checkcast #48 // class Thingy
// 31: astore_3
val b8 = Thingy.good2(42)
// 32: getstatic #19 // Field Thingy$.MODULE$:LThingy$;
// 35: ldc #43 // String 1337
// 37: invokevirtual #47 // Method Thingy$.bad1:(Ljava/lang/String;)LThingy;
// 40: astore 4
val c = Thingy.bad1("1337")
// 32: getstatic #26 // Field Thingy$.MODULE$:LThingy$;
// 35: ldc #50 // String 1337
// 37: invokevirtual #54 // Method Thingy$.bad1:(Ljava/lang/String;)LThingy;
// 40: astore 4
val c8 = Thingy.bad1("1337")
// 42: getstatic #19 // Field Thingy$.MODULE$:LThingy$;
// 45: invokevirtual #50 // Method Thingy$.bad2:()Lscala/Function1;
// 48: ldc #43 // String 1337
// 50: invokeinterface #39, 2 // InterfaceMethod scala/Function1.apply:(Ljava/lang/Object;)Ljava/lang/Object;
// 55: checkcast #41 // class Thingy
// 58: astore 5
val d = Thingy.bad2("1337")
// 42: getstatic #26 // Field Thingy$.MODULE$:LThingy$;
// 45: invokevirtual #57 // Method Thingy$.bad2:()Lscala/Function1;
// 48: ldc #50 // String 1337
// 50: invokeinterface #46, 2 // InterfaceMethod scala/Function1.apply:(Ljava/lang/Object;)Ljava/lang/Object;
// 55: checkcast #48 // class Thingy
// 58: astore 5
val d8 = Thingy.bad2("1337")
// 60: new #52 // class Blippy$$anonfun$4
// 63: dup
// 64: invokespecial #53 // Method Blippy$$anonfun$4."<init>":()V
// 67: new #55 // class Blippy$$anonfun$5
// 70: dup
// 71: invokespecial #56 // Method Blippy$$anonfun$5."<init>":()V
// 74: invokeinterface #60, 2 // InterfaceMethod scala/Function1.andThen:(Lscala/Function1;)Lscala/Function1;
// 79: bipush 42
// 81: invokestatic #25 // Method scala/runtime/BoxesRunTime.boxToInteger:(I)Ljava/lang/Integer;
// 84: invokeinterface #39, 2 // InterfaceMethod scala/Function1.apply:(Ljava/lang/Object;)Ljava/lang/Object;
// 89: checkcast #62 // class java/lang/String
// 92: astore 6
val e = (Thingy.good1[Int] _).andThen(_.bla("Bernd"))(42)
// 60: invokedynamic #78, 0 // InvokeDynamic #0:apply:()Lscala/compat/java8/JFunction1;
// 65: invokedynamic #85, 0 // InvokeDynamic #1:apply:()Lscala/compat/java8/JFunction1;
// 70: checkcast #42 // class scala/Function1
// 73: invokeinterface #89, 2 // InterfaceMethod scala/Function1.andThen:(Lscala/Function1;)Lscala/Function1;
// 78: bipush 42
// 80: invokestatic #32 // Method scala/runtime/BoxesRunTime.boxToInteger:(I)Ljava/lang/Integer;
// 83: invokeinterface #46, 2 // InterfaceMethod scala/Function1.apply:(Ljava/lang/Object;)Ljava/lang/Object;
// 88: checkcast #91 // class java/lang/String
// 91: astore 6
val e8 = (Thingy.good1[Int] _).andThen(_.bla("Bernd"))(42)
// 94: getstatic #19 // Field Thingy$.MODULE$:LThingy$;
// 97: invokevirtual #33 // Method Thingy$.good2:()Lscala/Function1;
// 100: new #64 // class Blippy$$anonfun$6
// 103: dup
// 104: invokespecial #65 // Method Blippy$$anonfun$6."<init>":()V
// 107: invokeinterface #60, 2 // InterfaceMethod scala/Function1.andThen:(Lscala/Function1;)Lscala/Function1;
// 112: bipush 42
// 114: invokestatic #25 // Method scala/runtime/BoxesRunTime.boxToInteger:(I)Ljava/lang/Integer;
// 117: invokeinterface #39, 2 // InterfaceMethod scala/Function1.apply:(Ljava/lang/Object;)Ljava/lang/Object;
// 122: checkcast #35 // class scala/Function1
// 125: ldc #67 // String Bernd
// 127: invokeinterface #39, 2 // InterfaceMethod scala/Function1.apply:(Ljava/lang/Object;)Ljava/lang/Object;
// 132: checkcast #62 // class java/lang/String
// 135: astore 7
val f = Thingy.good2[Int].andThen(_.bla2)(42)("Bernd")
// 93: getstatic #26 // Field Thingy$.MODULE$:LThingy$;
// 96: invokevirtual #40 // Method Thingy$.good2:()Lscala/Function1;
// 99: invokedynamic #98, 0 // InvokeDynamic #2:apply:()Lscala/compat/java8/JFunction1;
// 104: checkcast #42 // class scala/Function1
// 107: invokeinterface #89, 2 // InterfaceMethod scala/Function1.andThen:(Lscala/Function1;)Lscala/Function1;
// 112: bipush 42
// 114: invokestatic #32 // Method scala/runtime/BoxesRunTime.boxToInteger:(I)Ljava/lang/Integer;
// 117: invokeinterface #46, 2 // InterfaceMethod scala/Function1.apply:(Ljava/lang/Object;)Ljava/lang/Object;
// 122: checkcast #42 // class scala/Function1
// 125: ldc #100 // String Bernd
// 127: invokeinterface #46, 2 // InterfaceMethod scala/Function1.apply:(Ljava/lang/Object;)Ljava/lang/Object;
// 132: checkcast #91 // class java/lang/String
// 135: astore 7
val f8 = Thingy.good2[Int].andThen(_.bla2)(42)("Bernd")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment