extended foreach?
Daniel Keep
daniel.keep.lists at gmail.com
Thu May 31 20:10:38 PDT 2007
Mandel wrote:
> Hi,
>
> I like to know if this feature request is even feasible
> and what others think of it, of course.
>
> class Foo {
> int opApply(int delegate(ref Type [, ...]) dg, float f, char[] s) {
> /*...*/
> }
> }
>
> float f = 1.5;
> char[] s = "abc";
> foreach(x; xs; foo; f, s)
> {
>
> }
>
> The idea is to pass additional values from foreach to opApply.
> Would be great to filter values in opApply for example.
> (or even chaining foreach in opApply...)
Usually, this is done by having a member function that returns a struct
(or class instance) that has an opApply on it. For instace:
class Foo {
struct FooIter {
float f;
char[] s;
int opApply(int delegate(ref Type [, ...]) dg) { /* ... */ }
}
FooIter myIter(float f, char[] s) {
return FooIter(f,s);
}
}
float f = 1.5;
char[] s = "abc";
foreach(x, xs; foo.myIter(f, s))
{
}
--
int getRandomNumber()
{
return 4; // chosen by fair dice roll.
// guaranteed to be random.
}
http://xkcd.com/
v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D
i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP http://hackerkey.com/
More information about the Digitalmars-d
mailing list