You are not logged in.
Pages: 1
i got an answer on another website, so i was under the impression that in this part
super.s = s
the parameter passed to the function
tb.new
replaces both
s=s
turns out it does not and only replaces the rvalue so
return s1.s...s2.s
works because the index of
super
is
s
not the value passed to the function and the table
super
is destroyed upon exit of the
tb.new
function and when it is called again the table is created again, so there are two different versions of the same table.
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.s
also as the
tb.new
function 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.
hi Xuer
For any division ('long' or 'short'), start by trying to divide into the first digit ie. 7 into 1. As this 'won't go' then try 7 into 16. This goes twice ie 2 units so the 2 goes above the 6.
Or think about a simple case such as 14 / 7. You know this is 2. 16 is a little bigger so the answer should be 2 and a bit. Doing a rough calculation first often helps.
Bob
thnx for your reply
Pages: 1