[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Alternative (better?) __index implementation
- From: Bradley Smith <gmane@...>
- Date: Mon, 26 Nov 2007 22:34:05 -0800
That is very interesting. I modified Lua in the way you describe, and I
get these results. Is these the same with what you are proposing?
----- example0.lua
-- Original example
local proxy = setmetatable({}, {__index = function(tab) return
tab.greeting end})
local test = setmetatable({ greeting = "hello" }, {__index = proxy})
print(test.unknownkey)
-- Lua: C stack overflow
-- Modified Lua: hello
-------------------------
----- example1.lua
-- Changed tab.greeting to rawget(tab, "greeting")
local proxy = setmetatable({}, {__index = function(tab) return
rawget(tab, "greeting") end})
local test = setmetatable({ greeting = "hello" }, {__index = proxy})
print(test.unknownkey)
-- Lua: nil
-- Modified Lua: hello
-------------------------
----- example2.lua
-- Set a greeting value in proxy
local proxy = setmetatable({greeting = "hi"}, {__index = function(tab)
return rawget(tab, "greeting") end})
local test = setmetatable({ greeting = "hello" }, {__index = proxy})
print(test.unknownkey)
-- Lua: hi
-- Modified Lua: hello
-------------------------
----- example3.lua
-- Removed greeting value in test
local proxy = setmetatable({greeting = "hi"}, {__index = function(tab)
return rawget(tab, "greeting") end})
local test = setmetatable({ }, {__index = proxy})
print(test.unknownkey)
-- Lua: hi
-- Modified Lua: nil