lua-users home
lua-l archive

Re: equivalent of Perl's "AUTOLOAD"

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


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

AltStyle によって変換されたページ (->オリジナル) /