[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Strange meta __newindex behavior
- From: David Haley <dchaley@...>
- Date: Sun, 25 Mar 2007 00:39:33 -0700
On this day of 03/24/07 23:28, Alan Hightower saw fit to scribe:
>
> No, the second case is independent of the first. When I perform
> root.sub1.foo = bar, sub1 doesn't exist in the root yet. After the line
> executes, it does, yet I never get called for the new index add. This
> seems broken to me.
Then something is missing from the story:
---------8<------------8<------------8<-------------------------
$ cat test.lua
root = {}
root._hidden_vals = {}
root._my_meta = {}
root._my_meta.__index = root._hidden_vals
root._my_meta.__newindex = _proc_lua_assignment
root._hidden_vals = function () print "index" end
root._proc_lua_assignment = function () print "newindex" end
setmetatable (root, root._my_meta)
root.sub1.foo = "bar"
---------8<------------8<------------8<-------------------------
$ lua test.lua
lua: test.lua:16: attempt to index field 'sub1' (a nil value)
stack traceback:
test.lua:16: in main chunk
[C]: ?
---------8<------------8<------------8<-------------------------
You have to have created the sub1 table first, before being able to
index sub1.foo. Otherwise, as we see here, sub1 is nil.
Cheers,
- David
--
~David-Haley
http://david.the-haleys.org