Hi, as the title says is there any way to declare record structures in Lua, such as in Visual Basic you would declare them like this:
Public Structure [name]
End Structure
They are pretty useful, so if there is any way that will be helpful.
Hi, as the title says is there any way to declare record structures in Lua, such as in Visual Basic you would declare them like this:
Public Structure [name]
End Structure
They are pretty useful, so if there is any way that will be helpful.
Disclaimer, I myself have never actually used visual basic before, this is pretty much based off of assumptions
I assume that you mean Classes and Structs here that are used for OOP, But there isn't a built in class constructor in lua, which means you would effectively have to make your own, using metatables, and the __index metamethod.
Skip to OOP section if you already know about metatables and metamethods
A metatable is simply put, a table. A table can be any other table's metatable, and a table can even be its own metatable (something used a lot in OOP, or object oriented programming)
You can set a table as the metatable of another table with the setmetatable function, and you can get the metatable of a table with the getmetatable function
For example:
local tbl = {}--Table:<memAddress1>
local meta = {}--Table:<memAddress2>
print(meta)--Table:<memAddress2>
setmetatable(tbl,meta)
print(getmetatable(tbl))--Table:<memAddress2>
The __index metamethod is fired when a script attempts to retrieve a nil value paired with a key in the table attached to the metatable.
For Example:
local tbl = {One = "Shedletsky", Two = "PopCorn"}--Table:<memAddress>
local meta = {__index = function(self,k)
print("Attempted to index key: "..k.. "in "..tostring(self))
end}
setmetatable(tbl,meta)
print(tbl.ree)--Attempted to index key: ree in Table:<memAddress>
print(tbl.Two)--PopCorn
The __index metamethod can also be a table that will also be searched through for the key selected, then return the value corresponding with the key provided, or nil
Ex.
local tbl = {One = "Shedletsky", Two = "PopCorn"}
local meta = {__index = {Thing = "Hi"}}
setmetatable(tbl,meta)
print(tbl.Thing)--Hi
print(tbl.Twooo)--nil
With the dull explanation out of the way, lets get to the fun stuff, OOP itself.
An example of OOP in lua:
Account = {balance = 0}
function Account:new (name)
local o = {name=name}
setmetatable(o, self)
self.__index = self
return o
end
function Account:deposit (v)
self.balance = self.balance + v
end
local acc = Account:new("BillBob")
acc:deposit(3)
print(acc.balance)--3
print(acc.name)--"BillBob"
You may be asking how this works, well, the Account table is the metatable of the acc table, and the __index table of itself. Meaning that if I look for a value that is nil in the acc table, for example the deposit function, it will loop at the __index metamethod of the metatable of the acc table.
The peculiar thing here is that, while deposit is a method of the Account table , or parent class, the self, when called by the sub-class acc is the value associated with self in the deposit method, you can test that yourself by printing self in the deposit method, then the acc table.
This passing on of methods and properties to sub-classes is called inheritance, and is one of the main uses of OOP.
This particular new method will also allow us to create sub-classes of sub-classes, and all sub classes of Account also inherit the new method , thus making things like this possible:
Account = {balance = 0}
function Account:new (name)
local o = {name=name}
setmetatable(o, self)
self.__index = self
return o
end
local acc = Account:new("BillBob")
local ac = acc:new("ree")
local a = ac:new("ding")
local b = a:new("Hello there")
Hopefully this helped!
Thank you this helped and I will be accepting this answer, it was very thorough.
This discussion is preserved as it was. New answers, comments, and votes are closed.
1 comment
The only data structure in lua is table. A struct has little to no meaning in a languages that has no type checking.