Luau - Proper Tail Calls / Recursion - Not Working

Based on this information: Programming in Lua : 6.3

Lua supports Proper Tail Calls

but when this function is tested in Roblox studio it unfortunately does not work


The code

function foo (n)
	if n > 0 then return foo(n - 1) end
end

foo(19998) -- make number higher if it does not error for you

print("End")

or

local function foo (n)
	if n % 1000 == 0 then print(n) task.wait() end
	if n > 0 then return foo(n - 1) end
end

foo(19997) -- make number higher if it does not error for you

The error

2 Likes

Tail calls were removed from Luau. I asked one of the engineers about this a few years ago and this was the response:


The solution would be to restructure algorithms to use loops instead of recursion.

3 Likes