www.digitalmars.com         C & C++   DMDScript  

D - template forward references/missing property bug or requirement

depending on where I put the
alias instance TreeWalk( Walker, Factory ) WalkStuff;
in this code I get
ttest4.d: class BaseFactory forward reference of base class Factory
// N.B. no line number
or
no property 'BaseWalker' for type 'instance TreeWalk(Walker,Factory)'
// N.B. no file

-------------

template TreeWalk( W, F ) {
	class BaseWalker {
		F parent;
		this( F f ) { parent = f; }
	}
	class BaseFactory : F {
		alias W delegate( F, ClassInfo ) getWalkDelegate;
		W getFrom( getWalkDelegate func, Object o ) {
			return func( this, o.classinfo );
		}
	}
}

//alias instance TreeWalk( Walker, Factory ) WalkStuff;
// this here gives
//ttest4.d: class BaseFactory forward reference of base class Factory

class Walker : WalkStuff.BaseWalker {
	this( Factory f ) { super( f ); }
}
class Factory {
	Walker getWalker( Factory f, ClassInfo ci ) { return null; }
}
alias instance TreeWalk( Walker, Factory ) WalkStuff;
//if here
//no property 'BaseWalker' for type 'instance TreeWalk(Walker,Factory)'

alias WalkStuff.BaseFactory ActualFactory;

int main( char[][] args ) {
	Factory f = new ActualFactory();
	Walker w = f.getFrom( f.getWalker, f );
	return w.classinfo.name.length;
}
----------------------------------------------
it can be changed to
(one form still causes the error see comments)

------------------------------------

template TreeWalk( W, F ) {
	class BaseWalker {
		F parent;
		this( F f ) { parent = f; }
	}
	class BaseFactory {
		this() { assert( this.classinfo === F.classinfo ); }
		alias W delegate( F, ClassInfo ) getWalkDelegate;
		F getSelf() { return cast(F)this; }
		W getFrom( getWalkDelegate func, Object o ) {
			return func( getSelf(), o.classinfo );
		}
	}
	
}

alias instance TreeWalk( Walker, Factory ) WalkStuff;
// this here gives works
class Walker : WalkStuff.BaseWalker {
	this( Factory f ) { super( f ); }
}
class Factory : WalkStuff.BaseFactory {
	Walker getWalker( Factory f, ClassInfo ci ) { return null; }
}
//alias instance TreeWalk( Walker, Factory ) WalkStuff;
// but here
// gives
//no property 'BaseWalker' for type 'instance TreeWalk(Walker,Factory)'

alias Factory ActualFactory;

int main( char[][] args ) {
	Factory f = new ActualFactory();
	Walker w = f.getFrom( f.getWalker, f );
	return w.classinfo.name.length;
}
Sep 15 2003