A use-case for an inline tailrec function is when you define a generic “sorted merge” algorithm over your datastructure with a custom fold function. tailrec allows to write the algorithm more cleanly, and inline is useful if the custom fold function wants an early return (i.e., if it’s a comparator).
Why is it prohibited? Looks like the compiler can first transform the function into a non-recursive form, and then inline it just like any other function.
Here’s some stupid code just for the sake of an example (doesn’t compile):
tailrec inline fun foo(n: Int, res: Int, mult: (Int, Int) -> Int): Int {
if (n == 1) return res
else return foo(n - 1, mult(n, res), mult)
}
My instict wants to say “how could it be possible to inline a tail recursive function”, because a true recursive function would not be possible to inline. But as mentioned that is not what happens when the code is compiled, so I am also a bit curious why this is not possible.
@baltiyskiy can you ellaborate what you mean with the non-local return?