lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Well, it keeps the type away from the actual data, so it's more
cosmetically than practically. It largely depends on what you want to
do - using metatables can be here an advantage or a disadvantage. An
advantage would be, that the actual record is "clean". A disadvantage
is, that you can't easily figure out what data you are looking at
unless you also look at the metatable. It's just another possibility
to identify what kind of data was passed that I wanted to point out.

Oh and another advantage would be, that you could create a kind of
class for each colortype with only little overhead, e.g.

rgb = setmetatable({},{__call=function(...) return rgb.new(...) end})
rgb.__mt = {__index = rgb, __tostring=function(...) return
rgb.tostring(...) end}
function rgb:new(r,g,b)
  return setmetatable({r,g,b},rgb.__mt)
end
function rgb:hsl()
  return -- actual code to return an hsl color
end
function rgb:tostring ()
  return ("rgb(%d,%d,%d)"):format(unpack(self))
end

print(rgb(30,40,50)) -- rgb(30,40,50) -- serialize = deserialize code

----
If each color class would provide an "rgb()" function to convert to
actual rgb, any function that fiddles with colors could easily
normalize the given color first into rgb code before continuing.

Cheers,
Eike

2010/2/24 spir <denis.spir@free.fr>:
> On Wed, 24 Feb 2010 08:48:55 +0100
> Eike Decker <zet23t@googlemail.com> wrote:
>
>> another possibility is to use "types" through using metatables to
>> identify certain tables
>
> This is very handy trick, indeed. But I don't understand the advantage of using metatables as type identifiers. Wouldn't any attribute do the job? For instance:
>
>> rgbtype = {}
>> hlstype = {}
>> function rgb(r,g,b) return setmetatable({r,g,b},rgbtype) end
>> function hsl(h,s,l) return setmetatable({h,s,l},hsltype) end
>
> function rgb(r,g,b) return {r,g,b, type=rgbtype) end
> function hsl(h,s,l) return {h,s,l, type=hsltype) end
>
>> function color (c)
>>   local t = getmetatable(c)
>>   if t == rgbtype then print "rgb"
>>   elseif t == hlstype then print "hls"
>>   else print "raw"
>>   end
>> end
>
> local t = c.type
>
>> color(rgb(4,5,6)) -- rgb
>> color(0xff3333) -- raw
>> color(hsl(0,0,5)) -- hls
>>
>> ----
>>
>> The metatable is carried along with the tables and identify the types
>> of the tables.
>
>> If you do not wish to use if ... elseif ... end structures in your
>> functions, you can also write a wrapping function that distincts
>> between metatable types in such manner:
>>
>> ---
>>
>> function polyfunc(t)
>>   return function (self,...)
>>     local tp = getmetatable(self) or 1
>>     return t[tp](self,...)
>>   end
>> end
>>
>> color = polyfunc {
>>   function (c) print "raw" end,
>>   rgbtype = function (c) print "rgb" end,
>>   hsltype = function (c) print "hsl" end
>> }
>>
>> color(rgb(0,5,5)) -- rgb
>> ----
>>
>> Though it won't really get shorter that way (after all, it's 3
>> different codepaths).
>
> Yes, thank you.
>
>> Cheers,
>> Eike
>>
>
> Denis
> ________________________________
>
> la vita e estrany
>
> http://spir.wikidot.com/
>