digitalmars.D - Recursive Declarations (for functions).
- Leandro Lucarella (22/22) Aug 24 2007 Hi. I was designing a FSM and I wanted to use a simple scheme where a
- 0ffh (4/5) Aug 24 2007 From the top of my head: No idea.
- 0ffh (29/30) Aug 24 2007 Ah, I just see in your link that you're also concerned
- Russell Lewis (3/21) Aug 25 2007 You're not the first to think that this is an intriguing concept!
- kris (11/33) Aug 25 2007 yeah, though D does support it via the combination of an typedef + alias...
- BCS (11/30) Aug 25 2007 ROFL
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
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
Leandro Lucarella wrote:http://www.gotw.ca/gotw/057.htmAh, 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
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
Russell Lewis wrote:Leandro Lucarella wrote: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.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
Reply to Leandro, ntHi. 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