Skip to content

Instantly share code, notes, and snippets.

@petitviolet
Last active January 9, 2017 11:53
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 petitviolet/bb0f14aad27021c46efecd972690d9b3 to your computer and use it in GitHub Desktop.
Save petitviolet/bb0f14aad27021c46efecd972690d9b3 to your computer and use it in GitHub Desktop.
[Scala]call-by-name

what is this

Diff of call-by-name and call-by-value.

summary

A call-by-name argument is converted to a Function0 and to get the value need to invoke it. Therefore, if a call-by-name argument is not used, a function using it is faster than it using call-by-value.

call-by-name

def byName(value: => String, flag: Boolean): String =
  if (flag) value else ""
  public byName(Lscala/Function0;Z)Ljava/lang/String;
   L0
    LINENUMBER 28 L0
    ILOAD 2
    IFEQ L1
    ALOAD 1
    INVOKEINTERFACE scala/Function0.apply ()Ljava/lang/Object;
    CHECKCAST java/lang/String
    GOTO L2
   L1
   FRAME SAME
    LDC ""
   L2
   FRAME SAME1 java/lang/String
    ARETURN
   L3
    LOCALVARIABLE this Lnet/petitviolet/sandbox/perf/CallByName; L0 L3 0
    LOCALVARIABLE value Lscala/Function0; L0 L3 1
    LOCALVARIABLE flag Z L0 L3 2
    MAXSTACK = 1
    MAXLOCALS = 3

call-by-value

def byValue(value: String, flag: Boolean): String =
  if (flag) value else ""
  public byValue(Ljava/lang/String;Z)Ljava/lang/String;
   L0
    LINENUMBER 31 L0
    ILOAD 2
    IFEQ L1
    ALOAD 1
    GOTO L2
   L1
   FRAME SAME
    LDC ""
   L2
   FRAME SAME1 java/lang/String
    ARETURN
   L3
    LOCALVARIABLE this Lnet/petitviolet/sandbox/perf/CallByName; L0 L3 0
    LOCALVARIABLE value Ljava/lang/String; L0 L3 1
    LOCALVARIABLE flag Z L0 L3 2
    MAXSTACK = 1
    MAXLOCALS = 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment