metamethods.rst 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. .. _metamethods:
  2. -----------
  3. Metamethods
  4. -----------
  5. Metamethods are a mechanism that allows the customization of certain aspects of the
  6. language semantics. Those methods are normal functions placed in a table
  7. parent(delegate) or class declaration; It is possible to change many aspects of a table/class instance behavior by just defining
  8. a metamethod. Class objects (not instances) support only 2 metamethods ``_newmember, _inherited`` .
  9. For example when we use relational operators other than '==' on 2 tables, the VM will
  10. check if the table has a method in his parent called '_cmp'; if so it will call it to determine
  11. the relation between the tables.::
  12. local comparable={
  13. _cmp = function (other)
  14. {
  15. if(name<other.name)return -1;
  16. if(name>other.name)return 1;
  17. return 0;
  18. }
  19. }
  20. local a={ name="Alberto" }.setdelegate(comparable);
  21. local b={ name="Wouter" }.setdelegate(comparable);
  22. if(a>b)
  23. print("a>b")
  24. else
  25. print("b<=a");
  26. for classes the previous code become: ::
  27. class Comparable {
  28. constructor(n)
  29. {
  30. name = n;
  31. }
  32. function _cmp(other)
  33. {
  34. if(name<other.name) return -1;
  35. if(name>other.name) return 1;
  36. return 0;
  37. }
  38. name = null;
  39. }
  40. local a = Comparable("Alberto");
  41. local b = Comparable("Wouter");
  42. if(a>b)
  43. print("a>b")
  44. else
  45. print("b<=a");
  46. ^^^^^
  47. _set
  48. ^^^^^
  49. ::
  50. _set(idx,val)
  51. invoked when the index idx is not present in the object or in its delegate chain.
  52. ``_set`` must 'throw null' to notify that a key wasn't found but the there were not runtime errors(clean failure).
  53. This allows the program to differentiate between a runtime error and a 'index not found'.
  54. ^^^^^
  55. _get
  56. ^^^^^
  57. ::
  58. _get(idx)
  59. invoked when the index idx is not present in the object or in its delegate chain.
  60. _get must 'throw null' to notify that a key wasn't found but the there were not runtime errors(clean failure).
  61. This allows the program to differentiate between a runtime error and a 'index not found'.
  62. ^^^^^^^^^
  63. _newslot
  64. ^^^^^^^^^
  65. ::
  66. _newslot(key,value)
  67. invoked when a script tries to add a new slot in a table.
  68. if the slot already exists in the target table the method will not be invoked also if the
  69. "new slot" operator is used.
  70. ^^^^^^^^^
  71. _delslot
  72. ^^^^^^^^^
  73. ::
  74. _delslot(key)
  75. invoked when a script deletes a slot from a table.
  76. if the method is invoked squirrel will not try to delete the slot himself
  77. ^^^^^^^^
  78. _add
  79. ^^^^^^^^
  80. ::
  81. _add(other)
  82. the + operator
  83. returns this + other
  84. ^^^^^^^^^^^^^^^^^^^^^^^^
  85. _sub
  86. ^^^^^^^^^^^^^^^^^^^^^^^^
  87. ::
  88. _sub(other)
  89. the - operator (like _add)
  90. ^^^^^^^^^^^^^^^^^^^^^^^^
  91. _mul
  92. ^^^^^^^^^^^^^^^^^^^^^^^^
  93. ::
  94. _mul(other)
  95. the ``*`` operator (like _add)
  96. ^^^^^^^^^^^^^^^^^^^^^^^^
  97. _div
  98. ^^^^^^^^^^^^^^^^^^^^^^^^
  99. ::
  100. _div(other)
  101. the ``/`` operator (like _add)
  102. ^^^^^^^^^^^^^^^^^^^^^^^^
  103. _modulo
  104. ^^^^^^^^^^^^^^^^^^^^^^^^
  105. ::
  106. _modulo(other)
  107. the ``%`` operator (like _add)
  108. ^^^^^^^^^
  109. _unm
  110. ^^^^^^^^^
  111. ::
  112. _unm()
  113. the unary minus operator
  114. ^^^^^^^^^^^^^^^^^^^^^^^^
  115. _tyoeof
  116. ^^^^^^^^^^^^^^^^^^^^^^^^
  117. ::
  118. _tyoeof()
  119. invoked by the typeof operator on tables, userdata and class instances
  120. returns the type of ``this`` as string
  121. ^^^^^^^^^^^^^^^^^^^^^^^^
  122. _cmp
  123. ^^^^^^^^^^^^^^^^^^^^^^^^
  124. ::
  125. _cmp(other)
  126. invoked to emulate the ``< > <= >=`` and ``<=>`` operators
  127. returns an integer as follow:
  128. +-----------+----------------------------+
  129. | returns | relationship |
  130. +===========+============================+
  131. | > 0 | if ``this`` > ``other`` |
  132. +-----------+----------------------------+
  133. | 0 | if ``this`` == ``other`` |
  134. +-----------+----------------------------+
  135. | < 0 | if ``this`` < ``other`` |
  136. +-----------+----------------------------+
  137. ^^^^^^^^^^^^^^^^^^^^^^^^
  138. _call
  139. ^^^^^^^^^^^^^^^^^^^^^^^^
  140. ::
  141. _call(other)
  142. invoked when a table, userdata, or class instance is called
  143. ^^^^^^^^^^^^^^^^^^^^^^^^
  144. _cloned
  145. ^^^^^^^^^^^^^^^^^^^^^^^^
  146. ::
  147. _cloned(original)
  148. invoked when a table or class instance is cloned(in the cloned table)
  149. ^^^^^^^^^^^^^^^^^^^^^^^^
  150. _nexti
  151. ^^^^^^^^^^^^^^^^^^^^^^^^
  152. ::
  153. _nexti(previdx)
  154. invoked when a userdata or class instance is iterated by a foreach loop
  155. if previdx==null it means that it is the first iteration.
  156. The function has to return the index of the 'next' value.
  157. ^^^^^^^^^^^^^^^^^^^^^^^^
  158. _tostring
  159. ^^^^^^^^^^^^^^^^^^^^^^^^
  160. ::
  161. _tostring(previdx)
  162. invoked when during string conacatenation or when the ``print`` function prints a table, instance, or userdata.
  163. The method is also invoked by the sq_tostring() api
  164. must return a string representation of the object.
  165. ^^^^^^^^^^^^^^^^^^^^^^^^
  166. _inherited
  167. ^^^^^^^^^^^^^^^^^^^^^^^^
  168. ::
  169. _inherited(attributes)
  170. invoked when a class object inherits from the class implementing ``_inherited``
  171. the ``this`` contains the new class.
  172. return value is ignored.
  173. ^^^^^^^^^^^^^^^^^^^^^^^^
  174. _newmember
  175. ^^^^^^^^^^^^^^^^^^^^^^^^
  176. ::
  177. _newmember(index,value,attributes,isstatic)
  178. invoked for each member declared in a class body(at declaration time).
  179. if the function is implemented, members will not be added to the class.