[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Deep Copy prototype
- From: "Wim Couwenberg" <w.couwenberg@...>
- Date: Wed, 7 May 2003 12:34:02 +0200
Why not give it a shot... Here's something to play with
Bye,
Wim
--8<--snip-snip--8<--snip-snip--8<--
function copy(x, cache)
if type(x) ~= "table" then
-- hum
return x
end
local t
if cache then
t = cache[x]
else
cache = {}
end
if not t then
t = {}
cache[x] = t
for k, v in pairs(x) do
t[copy(k, cache)] = copy(v, cache)
end
-- hum
setmetatable(t, getmetatable(x))
end
return t
end
-- test it!
x = {"aap", "noot", sub={x=10, y=20}}
x.self = {x, mies=10}
x.bus = x.sub
y = copy(x)
print(y[2])
print(y.self[1].bus.y)
if y.bus == y.sub then print "same!" end