trying to learn more about metatables and oop but from examples I see I get extremely confused. I got a few short scripts here and was wondering if somebody more savvy with oop knowledge could explain this
local module={}
module.__index=module
--[[
First off wtf does putting the module table back into its own index do. I roughly
understand that __index "Fires when table[index] is indexed, if table[index]
is nil." But what would be the point of this if the index table is already nil?
]]
function module:new()
self--What is self reference here. Is it referencing the module itself?
end
return module
Another thing that confuses me is whats the point of using metatables when you dont need them to use the metamethods. Are metatables purely to make organization simpler and metamethods to implenent unique ways to interact with objects
There is a whole resource about metatables if you want to look at that,
Simply:
It allows you to index the Table with self so when using setmetatable, you would create the metatable with this indexmetamethod, which would allow you to index it, and the current table.
self itself is just a Variable, there is nothing to it.
Line-by-line breakdown. (kinda bored so why not) local module = {} creates a new table and assigns it to the local variable module.
module.__index = module sets the __index field of the module table to itself. This means that when a field is accessed on the module table and that field doesn’t exist, Lua will look for the field in module.__index instead.
The function module:new() defines a method called new on the module table. When this method is called, self will refer to the table the method was called on.
Regarding your question about the __index field:
When you set module.__index = module, the module is set up to behave as if any undefined fields are present on the module table itself. This can be useful for providing default values or methods for fields that may not have been defined yet. When a method is called on a Lua table, the first argument passed to the method is always the table itself (often referred to as self), so the self in the module:new() function will refer to the module table.
It is referencing whatever the table the function is contained in. In your case, self = module. This is done when the colon operator is used with a table function. The dot operator’s equivalent in your case is function module.new(self).
I wouldn’t necessarily recommend this type of standard OOP infrastructure.
I’m not an OOP expert, but I believe that this specifically does nothing other than some typechecking stuff. The __index I have is typically inside my constructor function to make everything clean.
From my experience, I didn’t know this because when I tried to use them without a metatable before the code did not work correctly.
In my use case for my tycoon’s OOP structure, the metatable is created inside the constructor function in such a way that you cannot do module.new().new or something like that, so it is more organized.
the module.__index means what if a table has the meta table of module if the value is nil it’ll look inside the metatable
EXAMPLE
tab1={}
meta={}
meta.__index=meta
meta.hi='hi'
setmetatable(tab1,meta)
print(tab1.hi)--OUTPUT:hi ||| tab1.hi is nil and meta.hi is not
the self is automatically in the module
which means module:new()=module.new(self)is true when calling a function
so when we do tab:new()
it means tab.new(tab)
and because new doesnt exist in tab it will go the to meta