[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: equivalent of Perl's "AUTOLOAD"
- From: PA <petite.abeille@...>
- Date: Tue, 27 Jun 2006 17:40:29 +0200
On Jun 27, 2006, at 13:27, George Jempty wrote:
Is there an equivalent of Perl's "AUTOLOAD" in Lua? Autoload is a
method
that you implement that gets called when you call a method on an
object but
that method name has not been defined.
Here is a little example of a 'proxy' class [1] which does this:
local aProxy = LUProxy()
aProxy:foo()
aProxy:bar()
Any method which do not exists get forwarded to, well, the 'forward'
method.
Here is the bulk of the __index metamethod:
self.__index =
function( anObject, aKey )
local aValue = self[ aKey ]
if aValue == nil and aKey:byte( 1 ) ~= 95 then
aValue = function( anObject, ... )
return anObject:forward( aKey, ... )
end
setfenv( aValue, _forward )
end
return aValue
end
Cheers
--
PA, Onnay Equitursay
http://alt.textdrive.com/
http://dev.alt.textdrive.com/browser/lu/LUProxy.lua