Metatables
Metamethods allow us to change the behavior of a table by writing custom functions for operators, such as comparing objects and arithmetical operations. For example, let’s say we would like to overload the add functionality of our table object with a new function that adds up certain field. Normally, the addition operation isn’t valid on tables, but we can overwrite the metamethod __add
to perform whatever we need.
Arithmetic methamethods
The following are the methamethods available:
__add | Addition operator |
__mul | Multiplication operator |
__sub | Subtraction operator |
__div | Division operator |
__unm | Negation operator |
__pow | Exponentiation operator |
__concat | Concatenation operator |
Relational methamethods
The following are the relational methamethods available:
__eq | Equality |
__lt | Less than |
__le | Less than or equal to |
The function setmetatable
is used to set the metastable of a table:
local vuln1 = {criticity_level = 10, name="Vuln #1"} local vuln2= {criticity_level = 4, name="Vuln #2"} local mt...