You are not logged in.
i already asked this question on stackoverflow and accepted an answer i think i did not really understand the answer and i got some more question, im embarrassed to necro it so im posting it here
im learning lua and got to metatable part, in this example
local tb = {}
local meta = {}
function tb.new(s)
local super = {}
super.s = s
setmetatable(super,meta)
return super
end
function tb.add(s1,s2)
return s1.s..s2.s
end
meta.__add = tb.add
f= tb.new("W")
t= tb.new("E")
print(f+t)when compiler gets to
f = tb.new("W") i think this happens
function tb.new("W") super.W = W return super end so
print(f+t) looks like
print(super+super) how does
tb.add(s1,s1) find the fields of super table using
return s1.s..s2.salso as the
tb.newfunction is called twice and
setmetatable(super,meta) happens twice is there any difference between the first and second iteration? if any of the above are incorrect please correct me.
Last edited by Xuer (2021-05-23 03:35:24)
Offline
i got an answer on another website, so i was under the impression that in this part
super.s = sthe parameter passed to the function
tb.newreplaces both
s=sturns out it does not and only replaces the rvalue so
return s1.s...s2.sworks because the index of
superis
snot the value passed to the function and the table
superis destroyed upon exit of the
tb.newfunction and when it is called again the table is created again, so there are two different versions of the same table.
Offline