lua-users home
lua-l archive

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

On Tue, Jun 28, 2011 at 10:25 AM, Oscar Chinellato
<> wrote:
> official Lua team (in a robust and proper way), however, any hints on
> how to correctly implement this additional metamethod would be greatly
> appreciated.

This is for user-defined types, yes? Because objects-as-references is
the standard Lua way of thinking.  It is not (as you say) what
assignment means in a mathematical sense of course.

The __newindex metamethod does get you part of the way.  In fact, by
giving the global environment a suitable __newindex, you can get what
you want, although it will not work with local variables (which are
not table references)

So, if A is global (or in the current environment, since you can
customize this for your scripts) then

A = 1

can be made to fire __newindex on the environment, if A does not
actually exist.

So store it as _A and define a __index metamethod  to prepend '_' when
it's trying to resolve a symbol.

You now have a way to hook into both assignment and variable access.

However, this is not going to happen when passing in a value to a
function, I'm afraid.

steve d.