-
Posted by erdem on Fri, 18 Feb 2011 08:16:07
Hi there is an example in Chapter 4 Defining Basic Arithmetic Using ... subtitle.
tbl1 = {"alpha", "beta", "gamma"} tbl2 = {"delta", "epsilon", "zeta"} tbl3 = {} mt = {} setmetatable(tbl1, mt) setmetatable(tbl2, mt) setmetatable(tbl3, mt) function mt.___add(a,b) local result = setmetatable({}, mt) -- Copy table a in first for i = 1, #a do table.insert(result, a[i]) end -- Copy table b in second for i = 1, #b do table.insert(result, b[i]) end return result end add_test = tbl1 + tbl2
When i try to compile this example i get an error message like this:
lua -e "io.stdout:setvbuf 'no'" "metatable.lua" lua: deneme.lua:23: attempt to perform arithmetic on global 'tbl1' (a table value) stack traceback: metatable.lua:23: in main chunk [C]: ? Exit code: 1
Thanks. Very nice book. I wish you continued success.
-
Posted by jnwhiteh on Fri, 18 Feb 2011 20:42:24
You have three underscores on
___add
, and you only need two, i.e.__add
. -
Posted by erdem on Fri, 18 Feb 2011 23:09:49
Thank you very much :)