lua-users home
lua-l archive

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

If private data in objects is desirable, PIL touches on another approach to
objects very much akin to one of the common Scheme approaches: Use closures.
What you lose is object-oriented syntax and you potentially have to write a
complex dispatch function.

The dispatch function is harder to work around, but there are a variety of
extensions one could make to Lua to make closure-based objects integrate
better. Here are two rough proposals. I'm sure they have "bugs" but I think
either could be made workable.

1. Drop the pretense that obj:msg() is the same as obj.msg( obj ) and
observe that it compiles to a special opcode (SELF). This can then be used
to make obj:msg() translate to obj( "msg" ) when obj turns out to be a
function. This can be done at runtime as part of the SELF opcode. I don't
have a firm opinion as to whether or not it should really translate to obj(
"msg", obj ). It probably should unless functions have an easy way to
recover their own identities but that might also interfere with exploiting
the existence of a SELF opcode; or

2. Add metatable support for functions and introduce a __send metamethod
that is essentially the counterpart to __call. This is more expensive than 1
but makes it easy to add __index and __newindex metamethods to function
based objects. Furthermore, it would be nice if these supported GC