meta data for this page
  •  

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
lua:meta [2015/07/19 13:20] niziaklua:meta [2016/07/14 08:50] – [Metamethods] niziak
Line 32: Line 32:
 </code> </code>
  
-IF mettables are normal table, so it is possible to use the same table also as metatable to save some resources+If mettables are normal table, so it is possible to use the same table also as metatable to save some resources
 <code lua> <code lua>
 setmetatable (NormalTable, NormalTable) setmetatable (NormalTable, NormalTable)
 +</code>
 +
 +**setmetatable(table, metatable)** returns its first argument, so it is possible to simplify code below:
 +<code lua>
 +local NormalTable = {}
 +setmetatable (NormalTable, NormalTable)
 +</code>
 +to
 +<code lua>
 +local NormalTable = setmetatable ({}, {})
 </code> </code>
  
 ===== Metamethods ===== ===== Metamethods =====
-  * **%%__index%%** is called when key in table doesn't exists. **%%__index%%** can be a function or another table - fallback table. Fallback table can have metatable which points to another fallback table and so on. It is possible to create very long fallback table chain. +[[http://lua-users.org/wiki/MetatableEvents]] 
-  * **%%__newindex%%** is called when value to not existsing key (contains **nil**) is assigned. If key existsmethod is not called. + 
-  * **%%__metatable%%** +[[http://lua-users.org/wiki/MetamethodsTutorial]]   
 + 
 +  * **%%__index%%** is called when key in table is accessed. **%%__index%%** can be a function or another table - fallback table. Fallback table can have metatable which points to another fallback table and so on. It is possible to create very long fallback table chain. 
 +    * **%%__index%% = function (table, key)**, return value of fucntion is returned as result. 
 +    * **%%__index%% = table** 
 +    * to access table key without invoking %%__index%% methamethod use **rawget(table,key)** 
 + 
 +  * **%%__newindex%%** is called when new value is assigned to key key 
 +    * **%%__newindex%% = function (table, key ,value)** 
 +    to set new value without invoking methamethod, use **rawset(table, key, value)** 
 + 
 +  * **%%__metatable%%** when set, metatable is hidden. Value of **%%__metatable%%** is returned instead of original metatable.
   * **%%__call%%** - if somebady calls table as function, this metamethod will be called   * **%%__call%%** - if somebady calls table as function, this metamethod will be called
-  * **%%__tostring%%** - +  * **%%__tostring%%** - when **tostring(table)** is called 
 +  * **%%__len%%**
  
 ===== Synctatic sugar ===== ===== Synctatic sugar =====
Line 65: Line 87:
 class:method(1, 2, 3) class:method(1, 2, 3)
 class.method(t, 1, 2, 3) class.method(t, 1, 2, 3)
-</codE>+</code>