Recursive typedef
BCS
BCS at pathlink.com
Thu Oct 5 10:58:24 PDT 2006
Markus Dangl wrote:
> BCS schrieb:
>
>> 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();
>
>
> That's exactly what i use it for - it's almost functional programming :)
One interesting things about the delegate form is that it can be used to
make an *infinite* state machine.
struct state
{
state delegate(Stream) next;
}
struct nest
{
nest* root;
state scan(Stream ins)
{
nest* tmp;
state ret;
if(ins.eof && root !is null)
throw new Error("invalid");
switch(ins.getc)
{
case '{':
tmp = new nest;
tmp.root = this;
ret.next = &tmp.scan;
return ret;
case '}':
if(root is null)
throw new Error("invalid");
ret.next = &root.scan;
return ret;
default:
ret.next = &this.scan;
return ret;
}
}
}
More information about the Digitalmars-d-learn
mailing list