With this, do you need to put return superClass at the end of every file?
Not sure about what you’re asking here… Can you be more explicit, please ?
Also, what is __index?
\_\_index
is a methamethod that Lua uses when you ask for a missing key in a table while this table has a metatable having the key \_\_index
.
That’s the core for OOP and inheritance mechanisms.
Say that you have a base class, with methods, and an instance from this class.
As you might be aware of, an instance must share all its class methods.
class = {...}
class.foo = function(...) ... end
instance = {...}
instance.foo --\> nil
Now, if instance has table class
as its metatable, and class implements \_\_index
key, we have a different behaviour:
class = {...}
class.\_\_index = class
class.foo = function(...) ... end
instance = setmetatable({...},class)
instance.foo --\> valid
instance.foo[/code] is still [code]nil[/code], as function [code]foo[/code] does not exists inside table [code]instance[/code]. But, instead of yielding [code]nil[/code], Lua tries to lookup inside instance's metatable, which is [code]class[/code]. This metatable has a key named [code]\_\_index[/code], which means. Lookup where [code]\_\_.index[/code] redirects. So Lua looks inside table [code]class[/code], finds method [code]foo[/code] and then returns the result of the call [code]foo(...)
There’s a great tutorial here, about metatables.
PiL is the reference, though. [import]uid: 142361 topic_id: 30158 reply_id: 122072[/import]