lua-users home
lua-l archive

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


Graham,

I have been working on a library this last week, and I just did some experimenting with separating my metatable and method table, as suggested by Rici Lake. It made my library a few lines longer, but it is mostly unchanged.

As expected, none of the metamethods are accessible from the Lua side anymore. For example, you can do these:

#udata
udata:close()
u3 = u1 + u2

But not these:

udata:__len()
udata:__gc()
u3 = u1:__add(u2)

This is the behavior you want, right? I'll post my library, if you want an example. I just need to write some documentation first.

Mike


Graham Wakefield wrote:
Having a close() method may be a good idea for many uses of userdata, where freeing resources referred by the userdata can be independent of the userdata lifetime. In such cases, it makes sense to test against NULL for the userdata contents, etc. Calling udata:close() won't set the udata to nil, won't free the userdata itself, since this is under the control of the garbage collector.

But this is irrelevant to __gc. The __gc metamethod, as far as I understand, is supposed to be a kind of callback into C to let you know that the userdata has been collected, so you can free additional resources if necessary. It makes no sense to expose this method to Lua code.
Using __gc as a close() method confuses two distinct purposes.