[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Preserving tail call information for resend?
- From: Rob Hoelz <rob@...>
- Date: Fri, 8 May 2009 11:26:50 -0500
Hello fellow Lua users,
I'm working on a resend implementation for my prototypical object
system, but I have a slight problem because of tail calls. Observe the
following implementation:
local object = {}
function object:clone()
local o = {__index = self}
return setmetatable(o, o)
end
function object:parent()
return self.__index
end
function object:resend(...)
local name = debug.getinfo(2).name
local self = self:parent()
return self[name](self, ...)
end
local foo = object:clone()
local bar = foo:clone()
function foo:baz()
print 'In foo.baz'
return 5
end
function bar:baz()
print 'In bar.baz'
-- can't tail call resend!
-- ideally, this would be return self:resend()
local v = self:resend()
return v
end
print(bar:baz())
Since Lua doesn't preserve debug information across tail calls, resend
will fail if you try to tail call it. I'm tempted to change my Lua
implementation to preserve the name field in the case of method calls;
can anyone give me a good example of why this would be a *bad* idea?
Given no such examples, I'll go ahead and do it. The only alternative
I see is raising an error if a user tries to tail call resend, but
that's obviously suboptimal.
Thanks,
Rob Hoelz