lua-users home
lua-l archive

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



On Jun 28, 2008, at 4:04 AM, Patrick Donnelly wrote:

Implementing a type method for all classes seems like a sane solution
to this problem.

One way to look at this is to leverage Lua's module _NAME:

http://www.lua.org/manual/5.1/manual.html#5.3

Define your classes as module and query their _NAME attribute:

local function Type( anObject )
    if type( anObject ) == 'table' then
return anObject._NAME or ( getmetatable( anObject ) or {} )._NAME
    end

    return type( anObject )
end

http://dev.alt.textdrive.com/browser/HTTP/HTTPExtra.lua#L528

Additionally, one can systematically define a meaningful __tostring metamethod:

function meta:__tostring()
    return ( '%s/%s' ):format( self._NAME, self._VERSION )
end

"Lua gives you the power; you build the mechanisms."
-- Roberto Ierusalimschy, Programming in Lua, December 2003

--
PA.
http://alt.textdrive.com/nanoki/