Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
October 17, 2006 Error when using delegate in foreach | ||||
---|---|---|---|---|
| ||||
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? |
October 17, 2006 Re: Error when using delegate in foreach | ||||
---|---|---|---|---|
| ||||
Posted in reply to Max Samuha | 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 |
October 17, 2006 Re: Error when using delegate in foreach | ||||
---|---|---|---|---|
| ||||
Posted in reply to Max Samuha | 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))'.
|
October 17, 2006 Re: Error when using delegate in foreach | ||||
---|---|---|---|---|
| ||||
Posted in reply to Max Samuha | 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);
}
}
|
October 18, 2006 Re: Error when using delegate in foreach | ||||
---|---|---|---|---|
| ||||
Posted in reply to Karen Lanrap | On Tue, 17 Oct 2006 23:57:46 +0000 (UTC), Karen Lanrap
<karen@digitaldaemon.com> wrote:
>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);
> }
>}
Yes, right. Thanks a lot
|
Copyright © 1999-2021 by the D Language Foundation