Error when using delegate in foreach

Max Samuha maxter at i.com.ua
Tue Oct 17 14:29:32 PDT 2006


I've tried to use a delegate as aggregate in foreach:

void main()
{
	auto dg = delegate(inout int i)
		{
			static int j;
			if (j > 1000) return 1;
			i = j++;
			return 0;
		};

	foreach (int i; dg)
	{
		writefln(i);
	}
}

The code gives the error:

test.d(15): cannot implicitly convert expression (__foreachbody2) of
type int delegate(inout int i) to int

test.d(15): cast(int)__foreachbody2 is not an lvalue

What am I doing wrong?



More information about the Digitalmars-d-learn mailing list