Recursive typedef

BCS BCS at pathlink.com
Thu Oct 5 09:18:36 PDT 2006


Markus Dangl wrote:
> Hello,
> 
> I'm trying to write a function like that:
> 
> typedef ParseFn function(char[] s) ParseFn;
> 
> i.E. a function that returns a pointer to a new function of equal type. 
> DMD gives me the error "circular reference of typedef ParseFn". Of 
> course i could be using a "void *" as return type, but there must be a 
> better (typesafe) way... Has anyone tried this before?

yes, just last night (and a few months back)

Going by way of void* is what I did for a function


IIRC this works

=typedef void* function() State;
=State foo() { return cast(State)&foo; }

delegate are a bit worse

=struct state { state delegate() use; }
=
=struct sam
={
=	state bar()
=	{
=		state ret;
=		ret.use = &this.bar;
=		return ret;
=	}
=}




But it does make for a cool state machine!!!

State at = &seed;
while(null !is at) at = cast(State)at();

or

state at = &seed;
while(null !is at.use) at = cast(state)at.use();



More information about the Digitalmars-d-learn mailing list