www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - recursive delegate declaration

reply kris <foo bar.com> writes:
I have a situation whereby a delegate should return an instance of 
itself, to enable call chaining. Here's my attempt, which compiles and 
appears to work with dmd 161:

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

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

void main ()
{
    Bar consumer (char[] v)
    {
         return cast(Bar) &consumer;
    }

    emit (&consumer);
}
--------------

The above seems like a tad too much of a hack. Can anyone come up with a 
cleaner approach?
Jun 20 2006
next sibling parent BCS <BCS pathlink.com> writes:
kris wrote:
 I have a situation whereby a delegate should return an instance of 
 itself, to enable call chaining. Here's my attempt, which compiles and 
 appears to work with dmd 161:
 
 --------------
 alias Consume delegate(char[]) Bar;
 typedef Bar delegate (char[]) Consume;
 
 
 void main ()
 {
    Bar consumer (char[] v)
    {
         return cast(Bar) &consumer;
    }
 
    emit (&consumer);
 }
 --------------
 
 The above seems like a tad too much of a hack. Can anyone come up with a 
 cleaner approach?
 
That's better than what I used. I was using it for a state machine. State at = Seed; while(null !is at) at = at(); and used something like this: #struct foo #void main () If function can be made to work, cast through void* gets it done.
Jun 20 2006
prev sibling parent reply Walter Bright <newshound digitalmars.com> writes:
kris wrote:
 I have a situation whereby a delegate should return an instance of 
 itself, to enable call chaining. Here's my attempt, which compiles and 
 appears to work with dmd 161:
This is an old problem, going back to C. How do you declare a function type that returns itself? The only way is with a cast and a typedef (alias).
Jun 20 2006
parent reply kris <foo bar.com> writes:
Walter Bright wrote:
 kris wrote:
 
 I have a situation whereby a delegate should return an instance of 
 itself, to enable call chaining. Here's my attempt, which compiles and 
 appears to work with dmd 161:
This is an old problem, going back to C. How do you declare a function type that returns itself? The only way is with a cast and a typedef (alias).
Hehe - yeah :) The example given is fine for my own personal use, but I shouldn't expose that sort of thing as part of an API :) The easy and clean remedy is to use an interface instead, but in this case I was hoping to expose something "lightweight" instead. Any ideas for a practical resolution?
Jun 20 2006
parent Walter Bright <newshound digitalmars.com> writes:
kris wrote:
 The easy and clean remedy is to use an interface instead, but in this 
 case I was hoping to expose something "lightweight" instead. Any ideas 
 for a practical resolution?
No. But I am constantly surprised at the neato solutions the D programmers here think up for things!
Jun 20 2006