Recursive Declarations (for functions): Example

kris foo at bar.com
Sat Aug 25 13:21:14 PDT 2007


Russell Lewis wrote:
> Leandro Lucarella wrote:
>> Hi. I was designing a FSM and I wanted to use a simple scheme where a
>> state is represented with a function which returns a pointer to the next
>> state (function) when I realized there is no support in the language to
>> declare a function that returns a function with the same signature as
>> itself.
>>
>> Here is an explanation of the problem and suggested solutions for C++:
>> http://www.gotw.ca/gotw/057.htm
>>
>> AFAIK you can implement something similar in D, but D doesn't have a
>> "native" solution either.
>>
>> What I want to know is if it's even posible to have a nice syntax to
>> support this, something like:
>>
>> fp function() fp;
>>
>> Is the type system capable of handling this?
> 
> You're not the first to think that this is an intriguing concept! 
> However, the type system doesn't support it with any "nice" syntax.

yeah, though D does support it via the combination of an typedef + alias 
as illustrated below [1]:

#        alias Consume delegate(char[]) Bar;
#        typedef Bar delegate (char[]) Consume;

#        Consume emit (Consume consume)
#        {
#                return consume ("1") ("2") ("3") ("4");
#        }


[1] the code used to compile correctly, but I haven't tried it in a long 
time.



More information about the Digitalmars-d mailing list