Calling SUPER methods

5 views
Skip to first unread message

Klaas-Jan Stol

unread,
Aug 7, 2005, 10:54:26 AM8/7/05
to Internals List
Hi,

I was wondering if it is possible to call specific methods from a parent
PMC from another method in your (child) PMC, for some other object than
SELF.
My situation may be a bit complex, so I'll explain:

I have a LuaTable PMC, extending the Hash PMC.
I override the "add" method, because users may override the "add" method
for Tables (which normally does not exist). These user-overriden methods
are stored in yet another LuaTable. This other LuaTable, the "metatable"
of methods (like 'add', 'subtract', etc.) is stored as a property (setprop).

So, when trying to add 2 tables together, this is what happens:

1. "add" in LuaTable is called (because of "Px + Py")
2. while in add, check if this LuaTable (you could say "SELF") has a
property "metatable".
3. if it has such a metatable, check if it has a field "add" (this would
contain the user-defined add function for adding tables).
4. if is has not, give an error saying you can't add tables.


However, in step 3, the "get_pmc_keyed" method is called of this
'metatable' PMC (which was stored as the "metatable" property). Users
may *also* override _these_ methods (so, set/get_pmc_keyed). That would
mean, that while handling this "add" method, another user-overriden
method is called. This should not happen. So, what I'd like to do, is to
call the "get_pmc_keyed" in the Hash PMC, for this 'metatable' PMC.

So, instead of doing this:

VTABLE_get_pmc_keyed(INTERP, metatable, key);

which calls the get_pmc_keyed method of the LuaTable PMC, I'd like to
call this get_pmc__keyed method of the Hash PMC for this metatable object.

So, something along the lines of

SUPER.VTABLE_get_pmc_keyed(INTERP, metatable, key);
// call "get_pmc_keyed" in the Hash PMC for the object 'metatable'

I hope my question is clear. Is there any way to do this?


[[ A somewhat related question to this is: would it be handy to add
syntax to call an arbitrary method from a parent class? So, in pmc2c.pl,
it says it's possible to call a method in a parent class by means of:

SUPER(...)

but that only allows for calling the _same_ method (so, when in the
method "add" for example, you can only call SUPER's add, not SUPER's
subtract or something). It might be handy to be able to do something like:

SUPER.method(...)

Or is there any other way to do this?
]]

Thanks,

klaas-jan

Leopold Toetsch

unread,
Aug 8, 2005, 7:48:23 AM8/8/05
to Klaas-Jan Stol, Internals List
Klaas-Jan Stol wrote:
>
> Hi,
>
> I was wondering if it is possible to call specific methods from a parent
> PMC from another method in your (child) PMC, for some other object than
> SELF.

Yes, see below.

> My situation may be a bit complex, so I'll explain:
>
> I have a LuaTable PMC, extending the Hash PMC.
> I override the "add" method, because users may override the "add" method
> for Tables (which normally does not exist). These user-overriden methods
> are stored in yet another LuaTable. This other LuaTable, the "metatable"
> of methods (like 'add', 'subtract', etc.) is stored as a property
> (setprop).

Did you try to just used Parrot's MMD system instead of the double
indirection through properties and metatables? I.e. instead of putting
the user-defined 'add' sub into the metatable just 'store_global' the
sub into the LuaTable namespace.

> So, something along the lines of
>
> SUPER.VTABLE_get_pmc_keyed(INTERP, metatable, key);
> // call "get_pmc_keyed" in the Hash PMC for the object 'metatable'

Have a look at classes/orderedhash.pmc. There is e.g.:

Hash.SUPER();

or

Hash.SELF.get_integer_keyed_str(key);

As you know that the parent isa Hash PMC, this will work fine. And you
can of course just call the function too.

> Thanks,
>
> klaas-jan

leo

Reply all
Reply to author
Forward
0 new messages