corona - Difference between tables and Metatables in lua -


what difference between tables , metatables in corona.what types of metatables . how use , can use it? main purpose of using tables , metatables?

lua (which language corona based on) uses metatables different purposes.

the relevant entry in manual section 2.8. nice tutorial can found here or here.

a metatable table other, set metatable on table (which i'll call base table further on, make difference between 2 tables).

a metatable can contain anything, special keys (starting double underscore) interesting ones. values set keys in table called on special occasions. occasion depends on key. interesting are:

  • __index: used whenever key in base table looked up, not exist. can either contain table, in key looked instead, or function, passed original table , key. can used implementing methods on tables (oop style), redirection, fall through cases, setting defaults, etc etc
  • __newindex: used whenever new key assigned in table (which nil). if it's table, key assigned in table. if it's function, function passed original table, key , value. can used controlling access table, preprocessing data, redirection of assignments.
  • __call: enables set function called if use eg. table().
  • __add,__sub,__mul,__div,__mod used implement binary operations,
  • __unm used implement unary operations,
  • __concat used implementing concatenation (the .. operator)
  • __len used implementing length operator (#)
  • __eq,__lt,__le used implementing comparisons

a small thing know when using __index & co.: in methods, should use rawget , rawset in order prevent calling metamethod each time again, causing loop. small example:

t={1,2,3}  -- basetable mt={} -- metatable mt.__index=function(t,k)     print("__index event "..tostring(t).." key "..k)     return "currently unavailable" end mt.__newindex=function(t,k,v)     print("__newindex event "..tostring(t).." key: "..k.." value: "..v)     if type(k)=="string"         rawset(t,k,v:reverse())     else         rawset(t,k,v)     end end mt.__call=function(t,...)     print("call table "..tostring(t).." arguments: ".. table.concat({...},','))     print("all elements of table:")     k,v in pairs(t) print(k,v) end end setmetatable(t,mt)  t[4]="foo" -- run __newindex method print(t[5]) -- run __index method t("foo","bar") -- multiple fall through example: t={} mt={} mt2={} setmetatable(t,mt)  -- metatable on base table setmetatable(mt,mt2) -- second layer of metatable mt.__index=function(t,k) print('key '..k..' not found in '..namelookup[t]) return getmetatable(t)[k] end -- tries looking nonexistant indexes in mt. mt2.__index=mt.__index -- function written portably, reuse it.  t[1]='a' mt[2]='b' mt2[3]='c' namelookup={[t]="t",[mt]="mt",[mt2]="mt2"} print(t[1],t[2],t[3],t[4]) 

now these silly examples, can more complex stuff. take @ examples, take @ relevant chapters in programming in lua, , experiment. , try not confused ;)


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -