Avoiding abstract
Frits van Bommel
fvbommel at REMwOVExCAPSs.nl
Wed May 30 11:12:49 PDT 2007
Oliver Ruebenkoenig wrote:
> Hi everyone,
>
> the following code does what i want. I was wondering if the same out put can be produced without having the abstract char [] rawSymName(); in the Expr class? Thanks for any hints and thoughts,
It looks like rawSymName() does something very similar to
Object.toString(). So given that, why not reuse that one? As an added
bonus, your writefln() calls get shorter because it automatically calls
toString() on Object parameters.
---
import std.stdio;
Expr mySymbol;
public class Expr {
public
abstract Expr head();
}
class Symbol : public Expr {
public
this( char [] name ) {
this.itsName = name;
}
Expr head() { return mySymbol; }
char [] toString () {
return this.itsName;
}
private
char [] itsName;
}
int main( char [][] arg ) {
Expr myString;
mySymbol = new Symbol("Symbol");
myString = new Symbol("String");
writefln(mySymbol );
writefln(mySymbol.head() );
writefln(mySymbol.head().head() );
writefln(myString.head().head() );
return 0;
}
---
Of course, this isn't very helpful if you were already using toString
for another representation of your objects...
More information about the Digitalmars-d-learn
mailing list