Error when using delegate in foreach

Tom S h3r3tic at remove.mat.uni.torun.pl
Tue Oct 17 16:16:35 PDT 2006


Max Samuha wrote:
> 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 think that the problem is that 'dg' should be of type 'int 
delegate(int delegate(inout int))'.



More information about the Digitalmars-d-learn mailing list