Calling Super Methods

3 views
Skip to first unread message

Matt Diephouse

unread,
Jul 22, 2005, 7:32:17 PM7/22/05
to Perl 6 Internals
There's currently no way to call a superclass' method. I've just run
into this and leo suggested I sent a note to the list.

Here's what I want to do:

I have a ParrotClass (a class defined in PIR) that is derived from the
String class. I want to override it's set_string_native method to do
some processing before I store the value. My first attempt was this:

.sub __set_string_native method
.param string value
self = value
print "assign\n"
.end

Of course, that doesn't work: `self = value` just calls the method
again and you quickly hit the recursion limit. While I probably won't
be able to use the = syntax, I should be able to call the parent's
method (since I can't actually set the string value myself like I can
in PMC-land).

Something like this ought to work:

.sub __set_string_native method
.param string value
self.SUPER::__set_string_native(value)
print "assign\n"
.end

I don't really care how it looks really, as long as it's possible. Any
thoughts? It'd be nice to get this specced so that it can be
implemented.

Thanks.

--
matt diephouse
http://matt.diephouse.com

Leopold Toetsch

unread,
Jul 23, 2005, 7:58:26 AM7/23/05
to ma...@diephouse.com, Perl 6 Internals
Matt Diephouse wrote:
> There's currently no way to call a superclass' method. I've just run
> into this and leo suggested I sent a note to the list.
>
> Here's what I want to do:
>
> I have a ParrotClass (a class defined in PIR) that is derived from the
> String class. I want to override it's set_string_native method to do
> some processing before I store the value. My first attempt was this:
>
> .sub __set_string_native method
> .param string value
> self = value
> print "assign\n"
> .end

While we haven't something like:

> self.SUPER::__set_string_native(value)

... it's still possible to call super for some methods like the
__set_string_native, by extracting the first attribute:

.sub __set_string_native method
.param string s
$I0 = classoffset self, "MyClass"
$P0 = getattribute self, $I0
$P0 = s
.end

see also t/pmc/object-meths_30.pir (r8674)

The question ramains of course, why you should do that, as the inherited
method just does the same, that is - just don't declare
__set_string_native, *if* there isn't any difference in implementation.

leo

Klaas-Jan Stol

unread,
Jul 23, 2005, 11:46:25 AM7/23/05
to Leopold Toetsch, ma...@diephouse.com, Perl 6 Internals
Leopold Toetsch wrote:

I've been in situations that you would want to call the superclass'
constructor from the child's constructor, this is handy in situations
that you want to add some extra things to the constructor, and you don't
want to duplicate the code. So, in my opinion it would be handy.
A somewhat related point to this, as well as related to an earlier
question of mine wrt implementing PMCs in PIR; is it possible to access
the internal datastructure of the PMC in PIR? So, for example, if you
would want to set/get the integer value of a PMC in PIR, how would you
do that? (or will there be supporting syntax for that?)

klaas-jan


>
>
>


Leopold Toetsch

unread,
Jul 23, 2005, 3:19:49 PM7/23/05
to Klaas-Jan Stol, ma...@diephouse.com, Perl 6 Internals

On Jul 23, 2005, at 17:46, Klaas-Jan Stol wrote:

> I've been in situations that you would want to call the superclass'
> constructor from the child's constructor, this is handy in situations
> that you want to add some extra things to the constructor, and you
> don't want to duplicate the code. So, in my opinion it would be handy.

Yep

> A somewhat related point to this, as well as related to an earlier
> question of mine wrt implementing PMCs in PIR; is it possible to
> access the internal datastructure of the PMC in PIR? So, for example,
> if you would want to set/get the integer value of a PMC in PIR, how
> would you do that? (or will there be supporting syntax for that?)

Exactly as I"ve shown above with the string access. A ParrotObject
subclassed from a PMC has an instance of that PMC as it's first
attribute - named '__value'.
If the object is created from a class w/o a PMC parent (i.e. with the
newclass opcode) you have to implement all the vtables in PIR and store
the information in some attribute, you have to create too.

> klaas-jan

leo

Reply all
Reply to author
Forward
0 new messages