Color Constant Trick

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
xXxMoNkEyMaNxXx
Party member
Posts: 206
Joined: Thu Jan 10, 2013 6:16 am
Location: Canada

Re: Color Constant Trick

Post by xXxMoNkEyMaNxXx »

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.
User avatar
ejmr
Party member
Posts: 302
Joined: Fri Jun 01, 2012 7:45 am
Location: South Carolina, U.S.A.
Contact:

Re: Color Constant Trick

Post by ejmr »

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.
That sounds pretty strange. Do you (or anyone) have an example of code where __index causes this problem?
ejmr :: Programming and Game-Dev Blog, GitHub
南無妙法蓮華經
User avatar
xXxMoNkEyMaNxXx
Party member
Posts: 206
Joined: Thu Jan 10, 2013 6:16 am
Location: Canada

Re: Color Constant Trick

Post by xXxMoNkEyMaNxXx »

Code: Select all

local met={"lol",1,true}
function met:__index()
	return unpack(self)
end
setmetatable(met,met)
print(met.asdf)
Output:
>lol
User avatar
markgo
Party member
Posts: 190
Joined: Sat Jan 05, 2013 12:21 am
Location: USA

Re: Color Constant Trick

Post by markgo »

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

User avatar
xXxMoNkEyMaNxXx
Party member
Posts: 206
Joined: Thu Jan 10, 2013 6:16 am
Location: Canada

Re: Color Constant Trick

Post by xXxMoNkEyMaNxXx »

LOLWHY
User avatar
markgo
Party member
Posts: 190
Joined: Sat Jan 05, 2013 12:21 am
Location: USA

Re: Color Constant Trick

Post by markgo »

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.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Color Constant Trick

Post by bartbes »

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.
User avatar
ejmr
Party member
Posts: 302
Joined: Fri Jun 01, 2012 7:45 am
Location: South Carolina, U.S.A.
Contact:

Re: Color Constant Trick

Post by ejmr »

xXxMoNkEyMaNxXx wrote:

Code: Select all

local met={"lol",1,true}
function met:__index()
	return unpack(self)
end
setmetatable(met,met)
print(met.asdf)
Output:
>lol
That is awful and you are an awful person for writing it.

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.
ejmr :: Programming and Game-Dev Blog, GitHub
南無妙法蓮華經
Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests