An error is reported the level of the @tailrec, when using it at a top level def, in the main object of a worksheet:
import scala.annotation.tailrec
object worksheet {
@tailrec
def f(i: Int): Int =
if (i % 2 == 0)
i
else
f(i -1)
}
The reason is that the annotation is not correctly managed by the instrumentation. The code generated is the following:
@tailrec;$skip(87);
def f(i: Int): Int =
if (i % 2 == 0)
...
And it looks like there will be another problem when this is fixed, the worksheet doesn't allow the use of private or final on a top level def, which is required to be able to use @tailrec.
The workaround is to put the @tailrec inside a def, or in a different object.
My comment on a possible additionnal problem is wrong. private or final modifier cannot be used at this position, but it doesn't block using @tailrec if the code is correctly instrumented.
An error is reported the level of the
@tailrec
, when using it at a top level def, in the main object of a worksheet:The reason is that the annotation is not correctly managed by the instrumentation. The code generated is the following:
And it looks like there will be another problem when this is fixed, the worksheet doesn't allow the use of
private
orfinal
on a top level def, which is required to be able to use@tailrec
.The workaround is to put the
@tailrec
inside a def, or in a different object.