One way to look at foreach as not so bad

Tom S h3r3tic at remove.mat.uni.torun.pl
Sat Oct 21 02:48:40 PDT 2006


Julio César Carrascal Urquijo wrote:
> Tom S wrote:
>  > Yup, or
>>
>> obj.each in (int i) {
>>     writefln(i);
>> };
> 
> Well yes, "in" looks better but it doesn't work right now, "=" does.

Sure it works ! :)



import std.stdio;


class Foo {
	int[] data;

	struct Iter_each {
		Foo owner;
		void opIn(void delegate(int) dg) {
			foreach (int d; owner.data) {
				dg(d);
			}
		}
	}

	Iter_each each() {
		Iter_each res;
		res.owner = this;
		return res;
	}
}



void main() {
	Foo foo = new Foo;
	foo.data = [34, 2, 55, 23];

	foo.each in (int i) {
		writefln(i);
	};
}




--
Tomasz Stachowiak



More information about the Digitalmars-d-announce mailing list