Does Corona handle Lua "tail calls" correctly?

I already know why I shouldn’t do it, so I don’t ask anyone to waste their time telling me that. I just need to know if Corona handles tail calls correctly.

Thanks!
[import]uid: 5540 topic_id: 25593 reply_id: 325593[/import]

Dumb question: What’s a tail call? [import]uid: 19626 topic_id: 25593 reply_id: 103540[/import]

This is an example of a tail call, apparently:

function foo(data) {  
 a(data);  
 return b(data);  
}  

as opposed to

function foo(data) {  
 a(data);  
 x = b(data);  
 return x;  
}  

No idea why you think you shouldnt do it.
I wouldn’t expect the compiler to optimise it by messing about with the stack, though.

Unless you are thinking of the lua equivalent of ‘GOTO’? [import]uid: 108660 topic_id: 25593 reply_id: 103555[/import]

Lua handles tail calls correctly. There’s no reason to suspect Corona has done anything to Lua to undermine this functionality. [import]uid: 44647 topic_id: 25593 reply_id: 103591[/import]

I have to ask, whats the purpose and when is it good to use this? [import]uid: 81188 topic_id: 25593 reply_id: 103603[/import]

Tail calls are a good thing. If you can use them and they make sense, why shouldn’t you?

Tail call elimination is a core Lua feature. I don’t think we did anything to break them. Do you have reason to think they don’t work in Corona? If you do, please let us know by filing a bug.
Tail calls are discussed in Programming in Lua (6.3). They are simply when the last operation in a function is to call another function. Stupid languages/compilers will simply call into the next function, leaving the current function on the stack. Then when that next function returns, you return to the caller function, which then realizes there is nothing left to do, and then it returns.

Smarter languages/compilers will realize that there is nothing to return to and optimize away these extra calls. This has an additional advantage in that the current function is not stored on the stack to be returned to. Languages have a limit on how many levels deep you can call into different functions. Recursive functions sometimes exceed these limits and cause problems because your program will halt/crash if you hit this limit. With tail call optimizations, these functions will not be put on the stack and will not count against you towards your limit.

There are a domain of problems that lend themselves very well to tail recursion so it is very useful.

PiL shows an example with a maze or MUD game (go to the north room, etc). Tail recursion is basically used as a simple GOTO without needing an actual goto.
[import]uid: 7563 topic_id: 25593 reply_id: 103618[/import]