www.digitalmars.com         C & C++   DMDScript  

D - is this a bug & with delegates ?

reply Mike Wynn <mike l8night.co.uk> writes:
I have the following tree walker class.

linux dmd 0.71
  gcc (GCC) 3.2.2 20030222 (Red Hat Linux 3.2.2-5)

if I do not put the `&` in front of the method name it does not work, 
but compiles fine.
i.e.
	register_walker( getFC, FunctionCall.classinfo ); // does not work
instead of
	register_walker( &getFC, FunctionCall.classinfo ); // works
which is suposed to be correct ?



class MyWF : WalkerFactory {
	Walker getFC( WalkerFactory wf ) {
		return new FCWalker( wf );
	}
	Walker getVar( WalkerFactory wf ) {
		return new VarWalker( wf );
	}
	Walker getConst( WalkerFactory wf ) {
		return new ConstWalker( wf );
	}
	Walker getOp( WalkerFactory wf ) {
		return new OpWalker( this );
	}
	this() {
		super();
		register_walker( &getFC, FunctionCall.classinfo );
		register_walker( &getVar, Var.classinfo );
		register_walker( &getConst, Const.classinfo );
		register_walker( &getOp, Operator.classinfo );
	}




// rest of main code.

class Node {
	Node[] children;
}

class WalkerFactory {
	alias Walker delegate( WalkerFactory ) CreateFunc;
	CreateFunc[ClassInfo] walkers;
	
	Walker getDef( WalkerFactory wf ) {
		return new Walker( wf );
	}
	
	this() {
		register_walker( &getDef, Node.classinfo );
	}
	void walk( Node n ) {
		CreateFunc cf = walkers[n.classinfo];
		if ( cf ) {
			Walker w = cf( this );
			if ( w ) { w.walk( n ); }
		}
	}
	void register_walker( CreateFunc w, ClassInfo ci ) {
		walkers[ci] = w;
	}
}

class Walker {
	WalkerFactory parent;
	this( WalkerFactory p0 ) { parent = p0; }
	void walk( Node n ) {
		Node c;
		if ( first( n ) ) {
			foreach( Node c; enum_children( n ) ) {
				if ( child_pre( n, c ) ) {
					parent.walk( c );
					child_post( n, c );
				}
			}
			last( n );
		}
	}
	Node[] enum_children( Node n ) { return n.children; }
	bit first( Node n ) { return true; }
	bit  child_pre( Node n, Node c ) { return true; }
	void child_post( Node n, Node c ) {	}
	void last( Node n ) {  }
}
class ReversedWalker : Walker {
	this( WalkerFactory p0 ) { super( p0 ); }
	Node[] enum_children( Node n ) { return n.children.dup.reverse; }
}
Sep 11 2003
parent reply "Walter" <walter digitalmars.com> writes:
It's hard to see as your example contains a lot of stuff in it that seems
not relevant to the problem. Can you cut it down please? Sometimes cutting
it down to the bare minimum to show the problem reveals it for what it is.
Sep 11 2003
next sibling parent Mike Wynn <mike l8night.co.uk> writes:
Walter wrote:
 It's hard to see as your example contains a lot of stuff in it that seems
 not relevant to the problem. Can you cut it down please? Sometimes cutting
 it down to the bare minimum to show the problem reveals it for what it is.
 
 
in the process of cutting it down ...
 main_forward.d: class MyWF forward reference of base class WalkerFactory
which is due to the Walker class having and error, (hence I've not the code down further, .... got to go out and buy food today).
Sep 12 2003
prev sibling parent reply Mike Wynn <mike l8night.co.uk> writes:
Walter wrote:
 It's hard to see as your example contains a lot of stuff in it that seems
 not relevant to the problem. Can you cut it down please? Sometimes cutting
 it down to the bare minimum to show the problem reveals it for what it is.
 
Arrggghh!! o.k. this is going to be one of those long time to find bugs, as soon as I cut the codesize down it works, (the whole tree walker is not that big do you want me to send you the whole original non working project ?)
Sep 12 2003
parent reply Mike Wynn <mike l8night.co.uk> writes:
Mike Wynn wrote:
 Walter wrote:
 It's hard to see as your example contains a lot of stuff in it that seems
 not relevant to the problem. Can you cut it down please? Sometimes 
 cutting
 it down to the bare minimum to show the problem reveals it for what it 
 is.
well now I'm stuck ... the original code will not break !! BUT who should a delegate be passed alias int delegate( int ) myfunc; void dofunc( myfunc f ) { .... } class MyObj { int funca( int a ) { ... } int funcb( int b ) { ... } void process() { dofunc( funca ); // <= is this right dofunc( &funcb ); // or is this right } } is there a difference (and if a sub class overrides funca or funcb will the `right` methods always be called (i.e. is it in the D spec that the address of a delegate is from the vtbl not direct)?)
Sep 12 2003
parent "Walter" <walter digitalmars.com> writes:
"Mike Wynn" <mike l8night.co.uk> wrote in message
news:bjstd3$qov$1 digitaldaemon.com...
 BUT who should a delegate be passed

 alias int delegate( int ) myfunc;
 void dofunc( myfunc f ) { .... }
 class MyObj {
 int funca( int a ) { ... }
 int funcb( int b ) { ... }
 void process() {
 dofunc( funca );   // <= is this right
 dofunc( &funcb ); // or is this right
The second one.
 }
 }

 is there a difference
The former no longer is supported.
 (and if a sub class overrides funca or funcb will
   the `right` methods always be called (i.e. is it in the D spec that
 the address of a delegate is from the vtbl not direct)?)
Yes, it comes from the vtbl[].
Nov 19 2003