[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Tag methods questions
- From: "Jay Carlson" <nop@...>
- Date: Fri, 27 Jul 2001 09:37:15 -0400
"Eric Ries" <firstname.lastname@example.org>
> I've got a quick tag method question. If I've got the "index" tag method
> for a table (say "foo"), I can get a function called whenever someone
> and member is nil. I can then dispatch this call based on knowledge of the
> index name (in this case the string "member").
> However, for the case of foo.member(bar) (or foo:member(bar)), what is the
> correct way to intercept this call? It seems to me that I lose the
> information that "someone tried to call function 'member' on table 'foo'"
> I use the function tag method.
> What is the 'correct' lua way to handle this case?
[oooh oooh I think I know this one]
Hook gettable. You have to return a function, but it doesn't have to be a
constant function. So:
function my_gettable(table, index)
local v = rawget(table, index)
if v then return v end
-- pretend all absent values are methods to be delegated or something
return function(self, ...)
assert(self == %table, "tried to call method from one object on a
print("would call", %table, "method name", %index)
The assert is there to avoid this kind of monkey business:
a = foo.method
a(bar) -- self doesn't match the table where we got the method from!
BTW, I think Lua is the fastest I've ever gone from zero knowledge to
writing metaobject protocols....