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.
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.
>> 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:
> 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
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?)
> 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.