Yeah true what is true OOP and false OOP if it does the same thing why should we consider them differently ?
Doing :
ClassTable = {
new = function(self,x,y)
local object = setmetatable({},{__index = self})
object.x = x
object.y = 2 * x
return object
end
}
newObject = ClassTable:new(2)
is essentially the same thing as doing :
class myClass():
def __init__(self,x,y):
self.x = x
self.y = 2 * x
myClassObject = myClass(2)
I wouldnât put a difference on them, what Iâm referring to is saying Lua doesnât have classes while saying Python and/or JS do; all 3 have support for it. OOP is the broader scope of the subject. If you can have a mutable state that can be used by functions, you ideally have OOP. Iâm debating against what was mentioned here Lua supposedly canât have classes while Python can.
Oh okay I (think I) get it, you mean that everything that could be used as OOP is OOP
Okay, in a way, but again I wasnât referring to OOP in general. OOP is very broad and can be applied almost anywhere; this just dragged on and away from what I was first discussing, which was:
Although lua does have built-in classes. It just doesnât let you create classes.
Actually I kinda donât get it are metatables classes ?
Metatables arenât really classes, theyâre a way that you can create custom classes.
Take a look at this article:
http://lua-users.org/wiki/MetamethodsTutorial
If you want an example of how metamethods can be used to âcreateâ classes, I used it in my party system. Hereâs the constructor for a âParty objectâ
function party.new(player)
local self = setmetatable({}, {
__index = party,
__tostring = function(self)
return self.leader.Name .. "'s Party. Members: " .. self:GetMemberCount()
end;
});
self.leader = player
self.members = {[player] = true}
AddParty(self) -- adds this party to a collection of parties so we can loop through them or something
return self
end
So first of all, all tables can have metatables.
Metatables are tables of customized methods which control what should happen when you try an unknown operation on the table.
For example: table + 1
invokes the __add
method in its metatable, and table()
invokes the __call
method.
For classes, whenever you index a table via table[index]
or table.index
, you invoke the __index
method. If the value assigned to this method is a table, then that table is indexed if the original table does not have it.
This is incorrect. Only tables explicitly given a metatable via setmetatable
have a metatable.
Metatables are basically classes right or are they an interpreted way to do it ?
Does it necessarily have to be a class? You could always use Tags via CollectionService instead.
They are an interpreted way to do it, pretty much, yeah.
There are several equivalent ways of emulating OOP in lua. Metatables are the correct way, this is the alternative:
Functions are equivalent to objects as you can return other functions from them (the technical term is a âclosureâ, read the Wikipedia article if you want to know more.) These are in some ways stronger than metatables because they allow you to have private properties and methods, but tend to be slower because Lua is optimized for metatables. Take the following constructor as an example
function Thing(prop1, prop2)
local self
local function PrivateMethod1(a)
print(self.publicprop3)
prop1 = prop1 + a
return prop1
end
self = {
publicprop3 = 0,
PublicMethod1 = function(b)
self.publicprop3 = self.publicprop3 + 1
return prop2 + PrivateMethod1(b)
end
}
return self
end
For usage:
local thing = Thing(2,3)
thing.publicprop3 = 5
thing.PublicMethod1(5)
If you need to extend a âclassâ I suggest you wrap and delegate instead (this applies to metatable based oop as well). Extension is unnecessary in a duck typed language like lua.
Although it does effectively the same thing itâs three time longer than using metatables or python âClassesâ
Itâs not quite equivalent but more an attempt of copying classes properties.
Lua is not an OOP language and doesnât have classes or inheritance. The Roblox platform is what makes the environment OOP. The workflow can be achieved in any language given you implement it yourself and with metatables you can get pretty close syntax wise as well. For further reading: PIL-16
That depends on what is being done. CollectionService is not necessarily an appropriate alternative for classes, depending on what it is youâre trying to accomplish.
(I went with a Wikipedia link that I thought was relevant because it seems to be a common location to go to. Feel free to look up resources or reading material on classes on the Internet that arenât Wikipedia - perhaps directly on sites for coding languages instead.)
No. Metatables themselves arenât classes; theyâre useful when it comes to object-oriented programming because of the __index
metamethod, which allows for inheritance. Using a prototype-based approach (wherein classes donât exist; everything is an object, and prototype objects act as the blueprint/template for other objects), the system usually falls along the lines of:
Code-wise, this would look something like:
-- Object
local Object = {}
function Object:new()
local o = {}
local mt = {__index = self}
setmetatable(o, mt)
return o
end
-- Object > Animal
local Animal = Object:new()
Animal.X = 0
function Animal:Walk(distance)
self.X = self.X + distance
end
-- Object > Animal > Dog
local Dog = Animal:new()
function Dog:Bark()
print("You hear a 'woof' from " .. self.X .. " studs away.")
end
-- example:
local dog1 = Dog:new()
local dog2 = Dog:new()
dog1:Walk(10)
dog1:Bark() --> You hear a 'woof' from 10 studs away.
dog2:Bark() --> You hear a 'woof' from 0 studs away.
Although this approach does not provide real privacy, it is commonly seen as the simplest and most lightweight method; I personally prefer it.
Yes, thatâs true metatable arenât class although they are similar.
They are not; they just provide extra functionality to data. Thereâs really no such thing as a âmetatableâ either - theyâre just a table that has been assigned the metatable of some datum through setmetatable
.