Interface and delegate D2.0
Oliver Rübenkönig
oliver.ruebenkoenigREM at web.de
Tue Mar 11 01:10:38 PDT 2008
Hello Everyone,
the following code segment works. However, I am not sure I understand why it does work. Also, I have some questions regarding way I did it.
I'd appreciate all sorts of comments you might have. Thanks.
---------------
import std.stdio;
//alias Expr delegate ( ) aFn;
alias Expr delegate ( Expr ) aFn;
interface Expr {
void print();
aFn analize();
}
Expr eval( Expr e, Expr env ) {
//return e.analize()();
return e.analize()( env );
}
class String : Expr {
public:
this( const char [] stringData ) { this.itsStringData = stringData; }
void print() { writef("\"", this.itsStringData,"\""); }
//aFn analize() { return { return cast(Expr)this; }; }
aFn analize() { Expr help( Expr env ) {
this.print();
return cast(Expr)env; }
return &help;
}
private:
const char [] itsStringData;
}
String makeString( const char [] stringData ) {
return new String( stringData );
}
int main( char[][] arg ) {
writefln();
Expr s = makeString("Wow");
Expr s2 = makeString("!");
s.print();
writefln();
//s.analize()().print();
s.analize()(s2).print();
writefln();
eval( s, s2 ).print();
writefln();
return 0;
}
---------------
1) In the String class is it possible to avoid the use of the "help" funcntion?
2) In the String class: why do I need to cast this to Expr - String is derived from Expr and should fit - should it not?
3) Is it possible to avoid the double brackets in Expr.analize()(Expr)? Something like Expr.analize(Expr)? I assume this could be done with an alias, are still the other ways? - well eval is another way. I think i am asking if you see a way to do the same thing with less brackets.
4) Of course all other comments are very welcome!
Thanks for your help,
Oliver
More information about the Digitalmars-d-learn
mailing list