Skip to content

Instantly share code, notes, and snippets.

@kikito
Created April 18, 2011 07:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kikito/924943 to your computer and use it in GitHub Desktop.
Save kikito/924943 to your computer and use it in GitHub Desktop.
automatically getting their name from _G
local function _getNameFromG(theClass)
for k,v in pairs(_G) do
if v == theClass then return k end
end
return "Anonymous" -- non-global classes will get "Anonymous"
end
function Object:subclass(name)
local theSubClass = {...}
...
-- assuming that your classDict is only used for the class, not the instances
...
classDict = {
__index = function(aClass, name)
if name=='name' then -- unnamed class
aClass.name = _getNameFromG(aClass)
return aClass.name
end
...
end
__newIndex = ...
}
setmetatable(theSubClass, classDict)
...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment