lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

>>> 04/30/03 10:38 AM >>>

> This only works for userdata objects as only the host 
> application would know how do collect them. Tables are 
> native Lua objects and thus don't need any help from 
> things like __gc event.
> Alex

I don't agree with this. In this particular case, for example, a
finalizer for the table allowed you to use the table as a wrapper for a
userdata. Without the ability to create a finalizer for the table, you
are forced to use a SECOND userdata as a wrapper to the userdata. In
general I'd rather work with tables than userdata when I can.

A finalizer is a way of doing some processing before an object is killed
off by the garbage collector. If you have an application where you need
to do some processing before the object is killed off, then you need a
finalizer. The object being a native Lua object or not isn't really the

For example you might have a table that stores a bunch of data that's
computationally expensive to recreate (a table of prime numbers comes to
mind). You could create a finalizer that saves the table to a disk file
before it is killed off. That way you get the most recent version saved
without having to explicitly save it.

I think that if you have a language structure that can contain other
elements (i.e. is changable over time) then you can come up with a
situation where it needs a finalizer. In Lua that's limited to tables
and userdata. Not having a finalizer for userdata is, in my mind, a flaw
in the language, but a rather minor one that can be worked around fairly

  - Tom Wrensch