Does anyone know how `tailrec` keyword works . I m...
# javadevelopers
r
Does anyone know how
tailrec
keyword works . I mean how it converts the tail call optimised recursion to iterative one
d
it uses a loop, and mutation (you can verify this by converting your code to bytecode and decompiling to java code)
apologies for super slow response, I was also tempted to say it uses
tailrec
r
Actually I was interested in procedure how it converts to loop and mutation. I have seen java bytecode
e
it should be converted to a loop which exits on return condition and has acc to memorize result;
Copy code
kotlin
tailrec fun fact(n: Int): Int {
if (n == 1) return 1
return n*fact(n-1)

fun fact_(n: Int): Int {
var acc = n
var ncopy = n
while (ncopy != 1) { // negates break recursion call condition
    acc = acc * ncopy
}
return acc
}