vals = { i=1, j=2}
setmetatable(vals, {
__add = function (a, b)
return a*b
end,
})
sr = vals.i+vals.j
print(sr)
It prints sr as 3. The expected answer is 2 as 1*2 equals to 2. Why the addition operation (metamethod) is not getting into picture from the metatable of vars?
You misunderstood that a table with a metatable only fires at the table.
...not at a key/value it holds with same or different datatype.
They have not inherited the metatable.
Exception: Own implemention with
__newindexWhere you can add/share the parent metatable to a new table (child then )
So look at this code and try it by yourself and understood...
"Numbers do not have metatables."
The datatype string has.
And the
__addis somewhat broken or not really useable...Imagine numbers have all math functions as methods...
Oups, how easy is this?