[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Object system
- From: Raffaele Salmaso <raffaele@...>
- Date: Wed, 08 Dec 2004 18:49:07 +0100
John Paquin wrote:
with your class system, is it possible to change the lua code (lass.lua
in this case), reload the lua file and expect to see the changes
reflected in existing objects (like c in this case)?
No, this version will not work.
I think that the only way to incorporate dynamic changes is to
dynamically (i.e. at the time of reference) follow the inheritance chain
backward to find the correct method.
You should use the non-caching version
---8<---
-- this version doesn't cache anything
-- beware that it is slower than the caching one
function class(super)
-- create a new class description
local klass = {}
-- set the superclass (for object inheritance)
setmetatable(klass, {
__index = super,
__call = function(self, ...)
local tmp = {}
setmetatable(tmp, klass)
if self.init then
self.init(tmp, unpack(arg))
end
return tmp
end
})
klass.__index = function(table, key)
local r = klass[key]
if type(r) == 'function' then
return function(...) return r(table, unpack(arg)) end
else
return r
end
end
return klass
end
object = class()
function object.__tostring(self)
return 'first version'
end
print(c)
function object.__tostring(self)
return 'second version'
end
print(c)
--->8---
this will print:
first version
second version
If you use the other version, you will get[1]:
first version
first version
The drawback is that is is slow, more slower than caching version.
See the other mails in this thread for some timing.
[1] in reality its all depend only when you call the method for the
first time: the first time you will freeze it.
--
()_() | Always keep the Titanic in mind when I talk | +----
(o.o) | about security or safety, meaning that nothin | +---+
'm m' | is fully secure. | O |
(___) | raffaele punto salmaso presso libero punto it
GPG fingerprint 1CF5 51D4 E528 EB87 9977 B277 BE8C BF1E 620C 40ED