www.digitalmars.com         C & C++   DMDScript  

D - This and Super

reply Joe Battelle <Joe_member pathlink.com> writes:
The documentation talks about using "this" as a placeholder for
constructors/destructors, and using the construct super(args) to call the
constructor of a super class.  What isn't stated explicitly is that this and
super also have their uses when defining other member functions, especially ones
that tack on funtionality to that provided in a base class.  There are some
caveats, however.

First off, the syntax for calling a superclass version of a method is as you
would expect: super.functionname().  There is an important limitation to super
though, you can't skip a generation; for example, you can't do this:
super.super.foo().  Nor can you work around this in the general case with a
method returning super called on same (e.g., super.duper().foo()).  You'll just
get the same object reference back--where you were in the inheritance graph is
lost; this can result in an infinite recursion.  I think overusing super (as in
Smalltalk) leads to unreadable and unmaintainable code so I'm not arguing for
change--just filling in documentation.

Example code:
--------------------
import c.stdio;
class A {
void foo() { printf("hi joe"); }
version(notwhatyoumightexpect) {
A duper() { return super; }
}
}
class B : A { void foo() { printf("["); super.foo(); printf("]"); }
class C : B { void foo() { printf("["); super.foo(); printf("]"); }
version(compilationerror) {
class C : B { void foo() { printf("["); super.super.foo(); printf("]"); }
}
version(infiniterecursion) {
class C : B { void foo() { printf("["); super.duper().foo(); printf("]"); }
}

int main(char[][] argv)
{
C c = new C;
c.foo(); // prints [[hi joe]]
return 0;
}
Aug 22 2002
parent reply Pavel Minayev <evilone omen.ru> writes:
On Fri=2C 23 Aug 2002 01=3A20=3A25 +0000 =28UTC=29 Joe Battelle 
=3CJoe=5Fmember=40pathlink=2Ecom=3E wrote=3A

=3E First off=2C the syntax for calling a superclass version of a method is as
you
=3E would expect=3A super=2Efunctionname=28=29=2E  There is an important
limitation to super
=3E though=2C you can't skip a generation=3B for example=2C you can't do this=3A
=3E super=2Esuper=2Efoo=28=29=2E  Nor can you work around this in the general
case with a

You should use the C++ syntax=3A

=09class Foo
=09{
=09=09void duh=28=29 { =2E=2E=2E }
=09}

=09class Bar=3A Foo
=09{
=09=09=2E=2E=2E
=09}

=09class Baz=3A Bar
=09{
=09=09void duh=28=29 { Foo=2Eduh=28=29=3B }=09=2F=2F note Foo instead of super
=09}
Aug 22 2002
parent reply Joe Battelle <Joe_member pathlink.com> writes:
The fact that you can use Class.foo() to choose a particular method may be
similar to C++, but it is not the C++ syntax (class::foo()).  When I saw the
super, I assumed Walter went the Smalltalk route.  This should be documented and
it is not in the current spec.

I really hope your duh() method name was not meant to be insulting.  I don't
really need to spend my spare time digging this crap up and posting it.
Aug 22 2002
parent reply Joe Battelle <Joe_member pathlink.com> writes:
I really hope your duh() method name was not meant to be insulting...
Sorry, I didn't mean to come off this pissy. My ego snagged at that one... Getting back to the documentation: the only place in the spec that I've found so far that shows Foo.bar() is under the Static Attributes section, and it's not spelled out that this syntax applies when bar is an instance method. In fact the spec has this to say: class Foo { static int bar() { return 6; } int foobar() { return 7; } } Foo.foobar(); // error, no instance of Foo Am I supposed to infer from this that if you put that same erroneous statement inside a method body it will work for instance methods? Well actually it doesn't because Foo is not a base class of Foo, but that's a different posting. It's that kind of detail that must be in the spec or it's worthless. As a language user I shouldn't have to guess about these things. As someone working with alpha software I don't mind having to figure it out, and post about it--and that's what I'm doing.
Aug 23 2002
next sibling parent Pavel Minayev <evilone omen.ru> writes:
On Fri, 23 Aug 2002 08:11:07 +0000 (UTC) Joe Battelle 
<Joe_member pathlink.com> wrote:

I really hope your duh() method name was not meant to be insulting...
Sorry, I didn't mean to come off this pissy. My ego snagged at that one...
=)
 Am I supposed to infer from this that if you put that same erroneous statement
 inside a method body it will work for instance methods?  Well actually it
 doesn't because Foo is not a base class of Foo, but that's a different 
posting. I'm not sure if it is in the specs, but I remember my discussion with Walter - I asked the same question, and got the reply - the one that I've posted...
Aug 23 2002
prev sibling parent "Walter" <walter digitalmars.com> writes:
I'll look into it and see what needs to be done. Thanks, -Walter
Aug 24 2002