Last active
August 29, 2015 14:06
-
-
Save lanceon/98da43a8789361c609ce to your computer and use it in GitHub Desktop.
Compiled and decompiled scala recursive methods with and without @tailrec annotation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> scalac Tailrec.scala | |
> javap -private -c Tailrec | |
Compiled from "Tailrec.scala" | |
public class main.Tailrec extends java.lang.Object{ | |
public int tail1(int); | |
Code: | |
0: iload_1 | |
1: bipush 10 | |
3: if_icmplt 10 | |
6: iload_1 | |
7: goto 17 | |
10: aload_0 | |
11: iload_1 | |
12: iconst_1 | |
13: iadd | |
14: invokevirtual #12; //Method tail1:(I)I | |
17: ireturn | |
private int tail2(int); | |
Code: | |
0: iload_1 | |
1: bipush 10 | |
3: if_icmplt 8 | |
6: iload_1 | |
7: ireturn | |
8: iload_1 | |
9: iconst_1 | |
10: iadd | |
11: istore_1 | |
12: goto 0 | |
public void main(java.lang.String[]); | |
Code: | |
0: aload_0 | |
1: iconst_0 | |
2: invokevirtual #12; //Method tail1:(I)I | |
5: pop | |
6: aload_0 | |
7: iconst_0 | |
8: invokespecial #21; //Method tail2:(I)I | |
11: pop | |
12: return | |
public main.Tailrec(); | |
Code: | |
0: aload_0 | |
1: invokespecial #27; //Method java/lang/Object."<init>":()V | |
4: return | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import scala.annotation.tailrec | |
class Tailrec { | |
def tail1(i: Int): Int = { | |
if (i >= 10) i | |
else tail1(i+1) | |
} | |
@tailrec | |
private def tail2(i: Int): Int = { | |
if (i >= 10) i | |
else tail2(i+1) | |
} | |
def main(args: Array[String]) { | |
tail1(0) | |
tail2(0) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment