lua-users home
lua-l archive

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

>   t.__index = c
>   setmetatable(t,t)
>   setmetatable(t,{__index=c})
>   setmetatable(t,c)

I use some combo of the 1st and 3rd--or maybe just the 3rd.  If I have
a metatable that I'm going to reuse, I usually follow the format:

local mt = {}
mt.__index = table
mt.__tostring = function (t, s) return table.concat(t, s or ", ") end
-- etc. --
setmetatable(table, mt)
-- and further settings --

In addition to the general format of above, I also use the idiom
    setmetatable(a, getmetatable(b))
quite often.

The 2nd example I do use on occasion, mostly when hacking
quick/throwaway scripts, and then I'll use the previously-mentioned
idiom to reuse the metatable if I need to.

(In other news, I'm new!  My name is Ben.  I've been lurking for a
couple weeks, and I saw this thread and thought, "Hey, something I can
actually comment on!"  I've been using Lua quite a lot recently, with
it just about surpassing Python by now.  I enjoy its clean simplicity
and excellent C API.  I guess I could say more, but I don't want to
get too OT, and I'm not sure about your policy on intro threads.  You
seem friendly enough, though!)