lua-users home
lua-l archive

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

Adrien de Croy wrote:
> Jerome Vuarand wrote:
>> Adrien de Croy wrote:
>>> but it doesn't really make that much sense that lua exposes all the
>>> other operators except assignment.
>> It does make sense if you consider the fact that assignment in Lua
>> are not expressions (contrary to C and many languages using C
>> syntax). *All* operators used in expressions are exposed.
> so all assignments are done by incrementing a reference count and
> referring to the rvalue instead of copying it? 
> If that's done in one place, it could be an opportunity for a
> metamethod call. 

Lua doesn't use reference counting, but a garbage collector mechanism.
However the effect is the same. The problem when you write a = b, is
that the object referenced by 'a' (if there is one) is not modified at
all, and has no knowledge of the variables referencing it, so it doesn't
make sense call a metamethod on it when 'a' is modified.

You could consider that the owner of 'a' could have a metamethod called.
When 'a' is a table field (t.a = b) or a global variable (a = b <=> _G.a
= b), __newindex is already called on the table. Finally when 'a' is a
local variable, all assignments to it are in the same scope as 'a'
creation, so adding behaviour after or before each of these assignments
is easily achieved manually.