I'm trying to make an operation on a structure tail recursive by building up a (custom) continuation but the compiler will not accept that my code is tail recursive. As soon as I try and declare a function literal that references the recursive function in the non-tail position it throws an error even if I am not calling the function here. The following is a much distilled dummy example of what triggers the error:
import scala.annotation.tailrec object Test extends App { @tailrec def tailrectest(i: Int): Int = i match { case i if i > 0 => { val x = () => tailrectest(10) tailrectest(i - 1) } case 0 => 0 } } I get
could not optimize @tailrec annotated method tailrectest: it contains a recursive call not in tail position It is referring to the line with val x = () => tailrectest(10)