Can a member function return a delegate to itself?
BCS
ao at pathlink.com
Thu May 24 10:05:09 PDT 2007
Reply to teales,
> I also think your solution is nifty. So the answer to my question is
> rather like what you suggested, but you have to add an opCall to S, as
> in:
>
> import std.stdio;
>
> struct S
> {
> S delegate(int) dg;
> S opCall(int n) { dg(n); return *this; }
> }
> struct O
> {
> int a;
> S sum(int i)
> {
> a += i;
> S ret;
> ret.dg = ∑
> return ret;
> }
> }
> void main(char[][] args)
> {
> O o;
> o.sum(1)(2)(3);
> writefln("%d", o.a); // prints 6 as desired
> }
> It was just a curious question in the first place but you never know,
> somebody might find a use for it.
>
I'd skipp the opCall step and do that as
> o.sum(1).dg(2).dg(3);
The use I'v found for it is in state mechines
S s;
while(s.go) s=s.dg(input);
More information about the Digitalmars-d
mailing list