Q: why can methods not be tail recursive? In the ...
# language-proposals
n
Q: why can methods not be tail recursive? In the following code, I would expect the second method definition to tail recursive, but it generates a warning: “Recursive call is not a tail call”.
Copy code
class Example(val parent: Example?) {
    // This is tail recursive
    tailrec fun foo(e: Example) {
        if (e.parent != null) foo(e.parent)
    }
    
    // But this is not and causes a compiler warning: "Recursive call is not a tail call"
    tailrec fun foo() {
        if (parent != null) parent.foo()
    }
}
Given that the class is closed and the call to
foo
not polymorphic, I’d have thought that there would be no difference between passing
parent
to the recursive call as an explicit parameter or the implicit
this
reference. Am I missing something? If not, could tail recursive, non-polymorphic instance methods be supported in a future language version?