Thread overview
recursive delegate declaration
Jun 20, 2006
kris
Jun 20, 2006
BCS
Jun 20, 2006
Walter Bright
Jun 20, 2006
kris
Jun 21, 2006
Walter Bright
June 20, 2006
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?

June 20, 2006
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
#{
#	foo delegate(char[]) bar;
#	static foo opCall(foo delegate(char[]) b)
#	{
#		foo ret;
#		ret.bar = b;
#		return ret;
#	}
#}
#
# void emit (foo consume)
# {
#     consume ("1").bar("2").bar("3").bar("4");
# }
#
#void main ()
#{
#   foo consumer (char[] v)
#   {
#        return cast() &consumer;
#   }
#
#   emit foo(&consumer);
#}


If function can be made to work, cast through void* gets it done.
June 20, 2006
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).
June 20, 2006
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?
June 21, 2006
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!