digitalmars.D.learn - Error when using delegate in foreach
- Max Samuha (20/20) Oct 17 2006 I've tried to use a delegate as aggregate in foreach:
- Carlos Santander (6/32) Oct 17 2006 I can't try it (Mac OS X here), but maybe your foreach should be:
- Tom S (3/29) Oct 17 2006 I think that the problem is that 'dg' should be of type 'int
- Karen Lanrap (21/22) Oct 17 2006 Semms you mean something like this:
- Max Samuha (3/25) Oct 18 2006 Yes, right. Thanks a lot
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?
 Oct 17 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
 Oct 17 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))'.
 Oct 17 2006
Max Samuha wrote:What am I doing wrong?Semms you mean something like this: import std.stdio; void main() { auto dg = delegate (int delegate( inout int) dgp) { static int j=0; int res=0; while( j < 15){ j++; res= dgp(j); if(res) break; } return res; }; foreach (int i; dg) { writefln(i); } }
 Oct 17 2006
On Tue, 17 Oct 2006 23:57:46 +0000 (UTC), Karen Lanrap <karen digitaldaemon.com> wrote:Max Samuha wrote:Yes, right. Thanks a lotWhat am I doing wrong?Semms you mean something like this: import std.stdio; void main() { auto dg = delegate (int delegate( inout int) dgp) { static int j=0; int res=0; while( j < 15){ j++; res= dgp(j); if(res) break; } return res; }; foreach (int i; dg) { writefln(i); } }
 Oct 18 2006








 
  
  
 
 Carlos Santander <csantander619 gmail.com>
 Carlos Santander <csantander619 gmail.com> 