Created
April 18, 2011 07:30
-
-
Save kikito/924943 to your computer and use it in GitHub Desktop.
automatically getting their name from _G
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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