Color Constant Trick
- xXxMoNkEyMaNxXx
- Party member
- Posts: 206
- Joined: Thu Jan 10, 2013 6:16 am
- Location: Canada
Re: Color Constant Trick
I've had problems with this idea in the past where __index truncates my returned values, so I don't think this would work. It is a great idea though.
Last edited by xXxMoNkEyMaNxXx on Fri Jan 25, 2013 5:37 am, edited 1 time in total.
- ejmr
- Party member
- Posts: 302
- Joined: Fri Jun 01, 2012 7:45 am
- Location: South Carolina, U.S.A.
- Contact:
Re: Color Constant Trick
That sounds pretty strange. Do you (or anyone) have an example of code where __index causes this problem?xXxMoNkEyMaNxXx wrote:I've had problems with this idea in the past where __index truncates my returned values in the past, so I don't think this would work.
- xXxMoNkEyMaNxXx
- Party member
- Posts: 206
- Joined: Thu Jan 10, 2013 6:16 am
- Location: Canada
Re: Color Constant Trick
Code: Select all
local met={"lol",1,true}
function met:__index()
return unpack(self)
end
setmetatable(met,met)
print(met.asdf)
>lol
Re: Color Constant Trick
From the Lua manual, it's implied that __index returns one value because of the parentheses.
Code: Select all
"index": The indexing access table[key].
function gettable_event (table, key)
local h
if type(table) == "table" then
local v = rawget(table, key)
if v ~= nil then return v end
h = metatable(table).__index
if h == nil then return nil end
else
h = metatable(table).__index
if h == nil then
error(···)
end
end
if type(h) == "function" then
return (h(table, key)) -- call the handler
else return h[key] -- or repeat operation on it
end
end
- xXxMoNkEyMaNxXx
- Party member
- Posts: 206
- Joined: Thu Jan 10, 2013 6:16 am
- Location: Canada
Re: Color Constant Trick
LOLWHY
Re: Color Constant Trick
The only logic I can think of is that it keeps the behavior of returning values consistent whether it's a function or a simple index.
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: Color Constant Trick
Why? Well, because it determines "the value" of a variable, which is always singular. Imagine how confusing it would be if your variable contained more than one thing.
- ejmr
- Party member
- Posts: 302
- Joined: Fri Jun 01, 2012 7:45 am
- Location: South Carolina, U.S.A.
- Contact:
Re: Color Constant Trick
That is awful and you are an awful person for writing it.xXxMoNkEyMaNxXx wrote:Output:Code: Select all
local met={"lol",1,true} function met:__index() return unpack(self) end setmetatable(met,met) print(met.asdf)
>lol
Heh, no I'm kidding. Thanks to you and bartbes for the example and explaining why it works the way it does. I have not used __index() in conjunction with unpack() before, but that's good to know for the future.
Who is online
Users browsing this forum: Bing [Bot], Google [Bot] and 6 guests