[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Alternative (better?) __index implementation
- From: "alex.mania@..." <alex.mania@...>
- Date: Thu, 29 Nov 2007 16:33:41 +0900
On Thu Nov 29 4:10 , 'Greg Falcon' sent:
>Unless I misunderstand your meaning, your proposal makes proxy tables
>work less like real tables, as they would now be unsuitable as a value
>for __index.
>
>For example:
>
>-- using http://lua-users.org/wiki/ReadOnlyTables:
>ro = readonlytable { greeting = 'Hi!' }
>test = setmetatable( {}, { __index = ro } )
>-- or, as another example: test = readonlytable(ro)
>print(test.greeting)
>
>-- Lua: Hi!
>-- Modified Lua: nil
Unless I've misinterpreted how readonlytable() works, I am pretty sure modified lua
would still print "Hi!". The change only modifies this behaviour:
tab = setmetatable({ greeting = "Hi!", goodbye = "Sayoonara" }, {__index = function
() return rawget(tab, "goodbye") end})
ro = readonlytable(tab)
test = setmetatable( { goodbye = "Ciao" }, {__index = ro} )
print(test.greeting)
print(test.unknownkey)
-- Lua: "Hi!", "Sayoonara"
-- Modified Lua: "Hi!", "Ciao"
So it is true that it would change the behaviour of where you use readonlytable()
on tables that have function __index metamethods, whether or not you could find
such a construct in reality would be another question altogether.
- Alex D