Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
August 23, 2002 This and Super | ||||
---|---|---|---|---|
| ||||
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; } |
August 23, 2002 Re: This and Super | ||||
---|---|---|---|---|
| ||||
Posted in reply to Joe Battelle | On Fri, 23 Aug 2002 01:20:25 +0000 (UTC) Joe Battelle <Joe_member@pathlink.com> wrote:
> 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
You should use the C++ syntax:
class Foo
{
void duh() { ... }
}
class Bar: Foo
{
...
}
class Baz: Bar
{
void duh() { Foo.duh(); } // note Foo instead of super
}
|
August 23, 2002 Re: This and Super | ||||
---|---|---|---|---|
| ||||
Posted in reply to Pavel Minayev | 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. |
August 23, 2002 Re: This and Super | ||||
---|---|---|---|---|
| ||||
Posted in reply to Joe Battelle | >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.
|
August 23, 2002 Re: This and Super | ||||
---|---|---|---|---|
| ||||
Posted in reply to Joe Battelle | 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... |
August 25, 2002 Re: This and Super | ||||
---|---|---|---|---|
| ||||
Posted in reply to Joe Battelle | I'll look into it and see what needs to be done. Thanks, -Walter |
Copyright © 1999-2021 by the D Language Foundation