1.15. Metamethods
Metamethods are a mechanism that allows the customization of certain aspects of the language semantics. Those methods are normal functions placed in a class declaration; It is possible to change many aspects of a table/class instance behavior by just defining a metamethod.
For example when we use relational operators other than ‘==’ on 2 instances, the VM will check if the class has a method in his parent called ‘_cmp’; if so it will call it to determine the relation between the objects.:
class Comparable {
constructor(n)
{
name = n;
}
function _cmp(other)
{
if(name<other.name) return -1;
if(name>other.name) return 1;
return 0;
}
name = null;
}
let a = Comparable("Alberto");
let b = Comparable("Wouter");
if(a>b)
print("a>b")
else
print("b<=a");
1.15.1. _set
_set(idx,val)
invoked when the index idx is not present in the object or in its delegate chain.
_set
must ‘throw null’ to notify that a key wasn’t found but the there were not runtime errors (clean failure).
This allows the program to differentiate between a runtime error and a ‘index not found’.
1.15.2. _get
_get(idx)
invoked when the index idx is not present in the object or in its delegate chain. _get must ‘throw null’ to notify that a key wasn’t found but the there were not runtime errors (clean failure). This allows the program to differentiate between a runtime error and a ‘index not found’.
1.15.3. _newslot
_newslot(key,value)
invoked when a script tries to add a new slot in a table.
if the slot already exists in the target table the method will not be invoked also if the “new slot” operator is used.
1.15.4. _delslot
_delslot(key)
invoked when a script deletes a slot from a table. if the method is invoked quirrel will not try to delete the slot himself
1.15.5. _add
_add(other)
the + operator
returns this + other
1.15.6. _sub
_sub(other)
the - operator (like _add)
1.15.7. _mul
_mul(other)
the *
operator (like _add)
1.15.8. _div
_div(other)
the /
operator (like _add)
1.15.9. _modulo
_modulo(other)
the %
operator (like _add)
1.15.10. _unm
_unm()
the unary minus operator
1.15.11. _typeof
_typeof()
invoked by the typeof operator on tables, userdata, and class instances.
Returns the type of this
as string
1.15.12. _cmp
_cmp(other)
invoked to emulate the < > <= >=
and <=>
operators
returns an integer as follow:
returns |
relationship |
---|---|
> 0 |
if |
0 |
if |
< 0 |
if |
1.15.13. _call
_call(other)
invoked when a table, userdata, or class instance is called
1.15.14. _cloned
_cloned(original)
invoked when a table or class instance is cloned(in the cloned table)
1.15.15. _nexti
_nexti(previdx)
invoked when a userdata or class instance is iterated by a foreach loop.
If previdx==null it means that it is the first iteration. The function has to return the index of the ‘next’ value.
1.15.16. _tostring
_tostring()
Invoked when during string concatenation or when the print
function prints a table, instance, or userdata.
The method is also invoked by the sq_tostring() API.
Must return a string representation of the object.
1.15.17. _lock
_lock()
Metamethods of class object only.
Called when a class is locked (manually by an explicit call to cls.lock()
, when the class is being inherited or when its instance is first created).
this
references the class object itself.
At this stage the class still can be modified.