[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: doubt about gc
- From: "Milano Carvalho" <milanogc@...>
- Date: Wed, 19 Dec 2001 21:32:34 -0200
I would like to know if when I execute the code shown below, the garbage
collector can "clean" the lost data.
x = {}
x[{}] = 1 -- lost data
I ask this because I am using this type of indexation to put into Private
table private attributes concerning an object, and when this object is
destructed I want that your private data also be destructed.
See an example (Public:new() is the function where is my doubt):
BEGIN
-- avoid redefine the package
if Account then
return
end
-- load dependencies
dofile("base.lua")
local Public = settag({}, newtag())
local Private = {}
Account = Public
settagmethod(tag(Public), "index", function(t, i)
-- return the Public field or try inherit from Base class
-- Lua 4: use rawget instead rawgettable
return rawgettable(%Public, i) or rawgettable(Base, i) -- or [to multiple
-- inherance]
end)
function Public:new()
local t = {}
settag(t, tag(%Public))
-- initialize private data
%Private[t] = {}
%Private[t].balance = 0
return t
end
function Public:deposit(v)
%Private.setBalance(self, %Private[self].balance + v)
end
function Public:withdraw(v)
%Private.setBalance(self, %Private[self].balance - v)
end
function Public:getBalance(v)
return %Private[self].balance
end
function Private:setBalance(v)
%Private[self].balance = v
end
--tests
local x = Account:new()
x:print(x:getBalance())
x:print(x.balance)
x:deposit(100)
local y = Account:new()
y:withdraw(10)
y:deposit(13)
y:print(y:getBalance())
y:print(x:getBalance())
END
The Base class is defined by the same way.
I'm using LuaOrb 2.0 that is Lua 3.2 based.
Regards,
Milano