May 24, 2007 Re: Can a member function return a delegate to itself? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to teales | 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); | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply