I am aware that Lua classes can be created using the OO system that Luabind exposes to Lua:
http://www.rasterbar.com/products/luabind/docs.html#defining-classes-in-lua
class 'lua_testclass'
function lua_testclass:__init(name)
self.name = name
end
function lua_testclass:print()
print(self.name)
end
a = lua_testclass('example')
a:print()
However I am unable to figure out how to scope the class within another namespace so I can do the following:
a = MyScope.lua_testclass('example')
a:print()
Anyone has a idea. I do not want my classes to pollute the global namespace in Lua.
Luabind's
classfunction will always pollute the global table. However, you can clean up after it:Then you would use it like this:
Analogous to
local mod = require 'mod'you have to spell the name of the class twice, but you could easily build another function on top of this that could be used likesetclass(MyScope, 'lua_testclass'), automatically putting the class intoMyScope:Disclaimer: All this code is entirely untested.