Skip to content

Instantly share code, notes, and snippets.

@xuwei-k
Created October 15, 2022 00:23
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 xuwei-k/a607227167201c23656b36f2b5240b31 to your computer and use it in GitHub Desktop.
Save xuwei-k/a607227167201c23656b36f2b5240b31 to your computer and use it in GitHub Desktop.
$ scala --version
Scala code runner version 2.13.10 -- Copyright 2002-2022, LAMP/EPFL and Lightbend, Inc.
$ scala -Wperformance -Xprint:jvm -e "def foo: Int = { var a = 10; @annotation.tailrec def loop(): Int = if (a == 0) 3 else { a -= 1 ; loop() } ; loop() }"
/var/folders/nz/vb2z3s8j7719gg71zgz9gj_40000gn/T/scalacmd2181293633408592115.scala:1: warning: Modification of variable a within a closure causes it to be boxed.
def foo: Int = { var a = 10; @annotation.tailrec def loop(): Int = if (a == 0) 3 else { a -= 1 ; loop() } ; loop() }
^
[[syntax trees at end of jvm]] // scalacmd2181293633408592115.scala
package <empty> {
object Main extends Object {
def main(args: Array[String]): Unit = {
new <$anon: Object>();
()
};
def <init>(): Main.type = {
Main.super.<init>();
()
}
};
final class anon$1 extends Object {
private def foo(): Int = {
var a: runtime.IntRef = scala.runtime.IntRef.create(10);
anon$1.this.loop$1(a)
};
@scala.annotation.tailrec final private[this] def loop$1(a$1: runtime.IntRef): Int = {
<synthetic> val _$this: <$anon: Object> = anon$1.this;
_loop(_$this: <$anon: Object>){
(if (a$1.elem.==(0))
3
else
{
a$1.elem = a$1.elem.-(1);
_loop(anon$1.this)
}: Int)
}
};
def <init>(): <$anon: Object> = {
anon$1.super.<init>();
()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment