www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Recursive Declarations (for functions).

reply Leandro Lucarella <llucax gmail.com> writes:
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?

-- 
LUCA - Leandro Lucarella - Usando Debian GNU/Linux Sid - GNU Generation
------------------------------------------------------------------------
E-Mail / JID:     luca lugmen.org.ar
GPG Fingerprint:  D9E1 4545 0F4B 7928 E82C  375D 4B02 0FE0 B08B 4FB2 
GPG Key:          gpg --keyserver pks.lugmen.org.ar --recv-keys B08B4FB2
------------------------------------------------------------------------
CONDUCTOR BORRACHO CASI PROVOCA UNA TRAGEDIA: BATMAN UNICO TESTIGO
	-- Crónica TV
Aug 24 2007
next sibling parent 0ffh <spam frankhirsch.net> writes:
Leandro Lucarella wrote:
 Is the type system capable of handling this?
From the top of my head: No idea. You can work around by returning a void pointer. Regards, Frank
Aug 24 2007
prev sibling next sibling parent 0ffh <spam frankhirsch.net> writes:
Leandro Lucarella wrote:
 http://www.gotw.ca/gotw/057.htm
Ah, I just see in your link that you're also concerned with "compatibility" and "type safety" issues. Well, maybe something along these lines will work for you: alias fptr function() func; struct fptr { func ptr; static fptr create(func ptr) { fptr fp; fp.ptr=ptr; return fp; } fptr opCall() { return ptr(); } } fptr test() { return fptr.create(&test); } void test2() { fptr y=test(); fptr z=y(); } Rergards, Frank
Aug 24 2007
prev sibling next sibling parent reply Russell Lewis <webmaster villagersonline.com> writes:
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.
Aug 25 2007
parent kris <foo bar.com> writes:
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]: [1] the code used to compile correctly, but I haven't tried it in a long time.
Aug 25 2007
prev sibling parent BCS <ao pathlink.com> writes:
Reply to Leandro,
nt
 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?
 
ROFL I was playing with almost exactly this yesterday and tried this alias R delegate() R; and found a bug in DMD where you get a stack overflow from it. DMD gives you a "recursive alias declaration" error (about 500+ of them actually) but I can think of no reason that it shouldn't be allowed to work. it would allow a hole slew of interesting things alias T[int] T; alias Container!(T) t;
Aug 25 2007