lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Fri, Jul 18, 2008 at 6:26 AM, Mike Crowe <drmikecrowe@gmail.com> wrote:

> 2)  I ran across something about tail calls, or tail recursion, does that
> apply?

You can use tail calls to approximate state machines, but it will mean
the system is no longer asynchronous in the same way and will run
until the terminating state is hit, unless you make use of coroutines.
It also doesn't help with your meaningful-names-for-debugging problem.
It does mean that you can use arbitrary arguments appropriate for the
state instead of always a single data argument, however.


--- code start

-- local variables need to be predeclared so they
-- can be referred to as upvalues before their own
-- function value is defined
local prepare, process1, process2

prepare = function(data)
	....
	return process1(newdata)
end

process1 = function(data)
	....
	return process2(newdata)
end

process2 = function(data)
	....
	return newdata
end

--- code end

Tail calls mean that one of these states could return with a call to
itself, an unlimited number of times, without overflowing the stack.