digitalmars.D - recursive delegate declaration
- kris (21/21) Jun 20 2006 I have a situation whereby a delegate should return an instance of
- BCS (32/55) Jun 20 2006 That's better than what I used. I was using it for a state machine.
- Walter Bright (3/6) Jun 20 2006 This is an old problem, going back to C. How do you declare a function
- kris (7/17) Jun 20 2006 Hehe - yeah :)
- Walter Bright (3/6) Jun 20 2006 No. But I am constantly surprised at the neato solutions the D
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
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
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
Walter Bright wrote:kris wrote: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?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
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