Skip to content

Instantly share code, notes, and snippets.

@kmizu
Created June 25, 2011 08:43
Show Gist options
  • Save kmizu/1046298 to your computer and use it in GitHub Desktop.
Save kmizu/1046298 to your computer and use it in GitHub Desktop.
Experiments about by-name parameter in Scala
class ByNameParams {
private def multiply(x: => Int) : Int = x * 2
def hello {
multiply(100)
}
}
>javap -c ByNameParams$$anonfun$hello$1
Compiled from "ByNameParams.scala"
//This class is generated by scalac
public final class ByNameParams$$anonfun$hello$1 extends scala.runtime.AbstractFunction0$mcI$sp implements java.io.Serializable{
public static final long serialVersionUID;
public static {};
Code:
0: lconst_0
1: putstatic #11; //Field serialVersionUID:J
4: return
public final int apply();
Code:
0: aload_0
1: invokevirtual #17; //Method apply$mcI$sp:()I
4: ireturn
public int apply$mcI$sp();
Code:
0: ldc #18; //int 100
2: ireturn
public final java.lang.Object apply();
Code:
0: aload_0
1: invokevirtual #21; //Method apply:()I
4: invokestatic #27; //Method scala/runtime/BoxesRunTime.boxToInteger:(I)Ljava/lang/Integer;
7: areturn
// The constructor of the anonymous function
public ByNameParams$$anonfun$hello$1(ByNameParams);
Code:
0: aload_0
1: invokespecial #33; //Method scala/runtime/AbstractFunction0$mcI$sp."<init>":()V
4: return
}
>javap -c ByNameParams
Compiled from "ByNameParams.scala"
public class ByNameParams extends java.lang.Object implements scala.ScalaObject{
public void hello();
Code:
0: aload_0
1: new #23; //class // Create anonymous function object generated by compiler.
4: dup //
5: aload_0 //
6: invokespecial #27; //Method ByNameParams$$anonfun$hello$1."<init>":(LByNameParams;)V // Invoke the constructor of the anonymous function generated by scalac.
9: invokespecial #31; //Method multiply:(Lscala/Function0;)I
12: pop
13: return
public ByNameParams();
Code:
0: aload_0
1: invokespecial #35; //Method java/lang/Object."<init>":()V
4: return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment