lua-users home
lua-l archive

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


On Thu, Apr 21, 2011 at 19:11, Linus Sjögren
<thelinx@unreliablepollution.net> wrote:
> If I'm understanding your question correctly, you're wondering why
> __newindex isn't called when assigning variables to keys that already exist.
> It does.
> If I'm not understanding your question correctly, you're wondering why there
> is no callback for when keys are accessed.
> That's what __index is for.
>
> ...or am I just completely wrong?
> On 22 April 2011 03:00, HyperHacker <hyperhacker@gmail.com> wrote:
>
>

The former, yes. Unless this has changed in 5.2?
Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> t={}; m={}
> m.__newindex=function(t,k,v) print('newindex',t,k,v) rawset(t,k,v) end
> setmetatable(t,m)
> t.a=1
newindex	table: 0x9145050	a	1
> t.a=1
>
I figured the reason was it would slow down table accesses, but I
wasn't certain. Although Axel makes a good point about using a bitflag
to tell whether the method is set.


-- 
Sent from my toaster.