Error when using delegate in foreach

Carlos Santander csantander619 at gmail.com
Tue Oct 17 15:43:13 PDT 2006


Max Samuha escribió:
> 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?

I can't try it (Mac OS X here), but maybe your foreach should be:
foreach (inout int i; dg)
Just a guess.

-- 
Carlos Santander Bernal



More information about the Digitalmars-d-learn mailing list