[q2x] OT: Lua questions

Brian Hook hook_l at pyrogon.com
Sat Jan 24 02:02:38 EST 2004


> I think if you want to duplicate the class you should do something
> like the following, which just copies all the members.
>
> classdef = {}
> function classdef:create()
> local new = {}
> for i, v in classdef do
> new[ i ] = classdef[ i ]
> end
> return new
> end

Right, that's what I was going to do, but the Wiki uses the metamethod 
technique, so I was left rather confused.  I'm currently using the 
metamethod technique with some success.

> The advantage of the metamethod solution is you only have one
> instance of the methods. The instance contains your member
> variables, but then you have a table which contains your methods
> (kind of like they're static). I suppose it's a bit like a vtable
> in C++. You can also then overload various operators, e.g. +, - etc.

If I use the metamethod solution, does that mean I can't overload?

c = classdef:create()
c.some_function = derived:somefunction

-- or whatever the syntax would be...

> If you run a file in a state, over a previous run then you'll
> probably destroy all the objects that were there, replace them and
> cause them to be garbage collected. 

That's what I feared. =|

Let's take a simple example.  Say I have a function like:

function object:do_something()
end


It's in its own file, object_do_something.lua.  No other objects are 
defined in there, so I don't have to trash any global variables.  Now, 
I edit and change that function.  The big question is (assuming 
nothing is in the middle of executing): will all previously created 
objects automagically reference the new object:do_something, or will 
they just vomit and die?

Thanks,

Brian





More information about the q2x mailing list