[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: settable
- From: Luiz Henrique de Figueiredo <lhf@...>
- Date: Thu, 12 Sep 2002 23:32:06 -0300
>I'm having some trouble with the settable event in Lua 5.0 (alpha).
As explained in the manual, __settable is never called when indexing a table.
Search for settable_event in the manual. (Same thing for __gettable.)
You can trace gettable/settable events on tables by setting up proxies
and using suitable __index and __newindex metamethods on the proxy.
But perhaps you should first consider whether you need to trace *all*
gettable/settable events, or only those that correspond to index/newindex,
in which case you can use them directly, of course.
We decided to change this in 5.0 because most uses of gettable/settable
in tables ended up doing rawget/set on the table, which was kludgy and
inefficient. On the other hand, __index and __newindex are called only
when needed. Thus, for instance, in the example below, you might *cache*
the values of y in x and __index would never be called again for cached
values. This is a useful technique for fiddling with global tables, for
example.
Here is a simple example. It creates a table x that is a (transparent)
proxy for the table y for getting values but which intercepts setting values.
Of course, in a real application, __newindex would do something useful.
% cat i
function trace_settable(x)
return setmetatable({},
{__index=x, __newindex=function (a,b,c) print("settable",a,b,c) end})
end
y={v=0}
x=trace_settable(y)
print(x.v,x.a)
x.a=100
% lua -v i
Lua 5.0 (alpha) Copyright (C) 1994-2002 Tecgraf, PUC-Rio
0 nil
settable table: 0x8062118 a 100
--lhf