lua-users home
lua-l archive

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


On Thu, Sep 28, 2006 at 03:35:32PM -0700, Wesley Smith wrote:
> Let's say I have
> 
> Obj = {}
> Obj_mt = { __index = function(t, k) -- do something
>                     end
>         }
> 
> function Obj.new(o)
>    setmetatable(o, Obj_mt)
>    return o
> end
> 
> obj1 = Obj.new{}
> 
> 
> When draw_circle is called, it looks up __index and falls the function
> defined in the metatable above where t = obj1 and k = "draw_circle".
> 
> My question is, how do I get at the arguments?  What happens to 0, 0,
> 0.5 and how can I access them?

You don't want to call draw_circle inside __index.  What you're really
doing here:

> obj1:draw_circle(0, 0, 0.5)

is this:

local func = Obj_mt.__index(Obj, "draw_circle");
func(obj1, 0, 0, 0.5);

so __index should *return* the function "draw_circle", not call it.

-- 
Glenn Maynard