English What are metatables?

5 replies
Goto Page
To the start Previous 1 Next To the start
21.02.18 08:47:54 pm
Up
script favor
User
Offline Off
My Questions Are

• What does mean metatables?
• Why we should we use metatables and when?
• It is importmant ?
• What we need to use for?
• Is good thing in LUA ?


I hope you answer my questions.
Visit my website : https://bounty-mafia.tk , Discord : https://discord.gg/ntNd6TX, IMG:http://i60.tinypic.com/5kixc8.gif
21.02.18 10:16:29 pm
Up
EngiN33R
Moderator
Offline Off
Metatables are a very powerful feature of the Lua language that allows you to change the behaviour of a table almost completely. Perhaps most importantly, metatables allow you to define custom "data types" and functions that act on them.

Metatables are a special kind of table that is associated with a certain normal table in Lua's internal code. Simple example of a very simple implementation of a 2D vector:

Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
local vector2_mt = {
    __add = function(a, b) -- Vector addition
        if (type(a) ~= type(b)) then error("type mismatch: 'table' and 'table' required, got '" .. type(a) .. "' + '" .. type(b) .. "'") end
        return vector2(a.x + b.x, a.y + b.y)
    end,
    __sub = function(a, b) -- Vector subtraction
        if (type(a) ~= type(b)) then error("type mismatch: 'table' and 'table' required, got '" .. type(a) .. "' + '" .. type(b) .. "'") end
        return vector2(a.x - b.x, a.y - b.y)
    end,
    __len = function(a) -- Vector length
        return math.sqrt(a.x^2 + a.y^2)
    end,
    __tostring = function(a) -- Vector serialisation
        return "{" .. a.x .. "," .. a.y .. "}"
    end
}

function vector2(x, y)
    local v = {x = x, y = y}
    setmetatable(v, vector2_mt)
    return v
end

local v1 = vector2(1, 1)
print(v1) -- {1, 1}
print(#v1) -- 1.4142135623731

local v2 = vector2(3, 3)
print(v2) -- {3, 3}

print(v1 + v2) -- {4, 4}
print(v2 - v1) -- {2, 2}
print(v1 + 1) -- error


This is a very powerful tool that allows you to make your code a lot shorter, more effective and more versatile. It comes at very little extra performance cost and is generally a "good thing", but does require some familiarity with Lua as a language to use. One common application of metatables is adding OOP-like behaviour to Lua, i.e. classes and objects.

If you're curious, there's more explanation and further examples on my site.
I code, therefore I exist. | Visit my blog for Lua tips and other interesting info
22.02.18 12:21:05 am
Up
Starkkz
Moderator
Offline Off
Lua itself doesn't support object oriented programming, and because of that, it supports metatables, which is a way to simulate object oriented programming, and a way to overload some operators.

Metatables do define a common pattern for objects, therefore they can be set over multiple tables.

If you like using class diagrams, this is a good way to give structure to your projects.

This is a technique I often use to define classes.
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
module("ClassName", package.seeall)

ClassName = {}
ClassName.__index = ClassName
ClassName.__type = "ClassName"

function ClassName:new(arg1, arg2, ...)
     
     local self = setmetatable( {}, ClassName )
     
     return self
     
end

return ClassName
lol
22.02.18 01:42:32 pm
Up
script favor
User
Offline Off
As i understand
Quote:
metatable is Every table and userdata object in Lua may have a metatable. This metatable is an ordinary Lua
table that defines the behavior of the original table and userdata under certain special operations.You can change several aspects of the behavior of an object by setting specific fields in its metatable.For instance, when an object is the operand of an addition, Lua checks for a function in the field "__add" in its metatable. If it finds one, Lua calls that function to perform the addition.We call the keys in a metatable events and the values metamethods. In the previous example,the event is "add" and the metamethod is the function that performs the addition.You can query and change the metatable of an object through the set/getmetatable functions.A metatable may control how an object behaves in arithmetic operations, order comparisons,concatenation, and indexing. A metatable can also define a function to be called when a userdata is garbage collected. For each of those operations Lua associates a specific key called an event.When Lua performs one of those operations over a table or a userdata, it checks whether that object has a metatable with the corresponding event. If so, the value associated with that key (the metamethod) controls how Lua will perform the operation.Metatables control the operations listed next. Each operation is identified by its corresponding name. The key for each operation is a string with its name prefixed by two underscores; for instance, the key for operation “add” is the string "__add". The semantics of these operations is better explained by a Lua function describing how the interpreter executes that operation.The code shown here in Lua is only illustrative; the real behavior is hard coded in the interpreter and it is much more efficient than this simulation. All functions used in these descriptions (rawget,tonumber, etc.) are described in. In particular, to retrieve the metamethod of a given object.
edited 4×, last 22.02.18 11:46:58 pm
Visit my website : https://bounty-mafia.tk , Discord : https://discord.gg/ntNd6TX, IMG:http://i60.tinypic.com/5kixc8.gif
22.02.18 11:44:50 pm
Up
Starkkz
Moderator
Offline Off
@user script favor: You should quote your sources.
http://pgl.yoyo.org/luai/i/2.8+Metatables
lol
22.02.18 11:46:45 pm
Up
script favor
User
Offline Off
@user Starkkz: My bad lol.
Visit my website : https://bounty-mafia.tk , Discord : https://discord.gg/ntNd6TX, IMG:http://i60.tinypic.com/5kixc8.gif
To the start Previous 1 Next To the start