lua-users home
lua-l archive

[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()


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, ... )

                        setfenv( aValue, _forward )

                return aValue


PA, Onnay Equitursay