Jump to page: 1 2
Thread overview
Foreach problem
Jan 11, 2009
Tim M
Jan 11, 2009
Daniel Keep
Jan 11, 2009
Tim M
Jan 11, 2009
Tim M
Jan 11, 2009
Bill Baxter
Jan 11, 2009
Tim M
Jan 11, 2009
Bill Baxter
Jan 11, 2009
Denis Koroskin
Jan 11, 2009
Tim M
Jan 11, 2009
Denis Koroskin
Jan 11, 2009
Tim M
Jan 11, 2009
Denis Koroskin
Jan 11, 2009
Tim M
Jan 12, 2009
Bill Baxter
Jan 11, 2009
Bill Baxter
January 11, 2009
Why is this an error. Dmd wants to make sure that I declare a new variable in the foreach statement and not use an existing one?

module test;

void main()
{
	int i;
	int[] nums;
	foreach(i; nums)
	{
		//
	}
}


dmd test.d

test.d(7): Error: shadowing declaration test.main.i is deprecated
January 11, 2009

Tim M wrote:
> 
> Why is this an error. Dmd wants to make sure that I declare a new variable in the foreach statement and not use an existing one?
> 
> module test;
> 
> void main()
> {
>     int i;
>     int[] nums;
>     foreach(i; nums)
>     {
>         //
>     }
> }
> 
> 
> dmd test.d
> 
> test.d(7): Error: shadowing declaration test.main.i is deprecated

Yes; as the error states, you're not allowed to define variables with the same name as variables in an enclosing scope any more.

  -- Daniel
January 11, 2009
On Sat, Jan 10, 2009 at 9:33 PM, Tim M <a@b.com> wrote:
>
> Why is this an error. Dmd wants to make sure that I declare a new variable in the foreach statement and not use an existing one?

You can't reuse existing variables as foreach loop indices, long story short.
January 11, 2009
On Sun, 11 Jan 2009 15:50:54 +1300, Daniel Keep <daniel.keep.lists@gmail.com> wrote:

>
>
> Tim M wrote:
>>  Why is this an error. Dmd wants to make sure that I declare a new variable in the foreach statement and not use an existing one?
>>  module test;
>>  void main()
>> {
>>     int i;
>>     int[] nums;
>>     foreach(i; nums)
>>     {
>>         //
>>     }
>> }
>>   dmd test.d
>>  test.d(7): Error: shadowing declaration test.main.i is deprecated
>
> Yes; as the error states, you're not allowed to define variables with the same name as variables in an enclosing scope any more.
>
>    -- Daniel

Why does it still work for some objects?
January 11, 2009
On Sun, 11 Jan 2009 15:59:26 +1300, Tim M <a@b.com> wrote:

> On Sun, 11 Jan 2009 15:50:54 +1300, Daniel Keep <daniel.keep.lists@gmail.com> wrote:
>
>>
>>
>> Tim M wrote:
>>>  Why is this an error. Dmd wants to make sure that I declare a new variable in the foreach statement and not use an existing one?
>>>  module test;
>>>  void main()
>>> {
>>>     int i;
>>>     int[] nums;
>>>     foreach(i; nums)
>>>     {
>>>         //
>>>     }
>>> }
>>>   dmd test.d
>>>  test.d(7): Error: shadowing declaration test.main.i is deprecated
>>
>> Yes; as the error states, you're not allowed to define variables with the same name as variables in an enclosing scope any more.
>>
>>    -- Daniel
>
> Why does it still work for some objects?


This works:


module test;

class A
{
	this()
	{
		//
	}
}

class B
{
	this()
	{
		//
	}
 	int opApply (int delegate (inout B) dg)
        {
                return 1;
        }
}

void main()
{
	A a;
	B b;
	foreach(a; b)
	{
		//
	}
}

January 11, 2009
On Sun, Jan 11, 2009 at 11:33 AM, Tim M <a@b.com> wrote:
>
> Why is this an error. Dmd wants to make sure that I declare a new variable in the foreach statement and not use an existing one?
>
> module test;
>
> void main()
> {
>        int i;
>        int[] nums;
>        foreach(i; nums)
>        {
>                //
>        }
> }
>
>
> dmd test.d
>
> test.d(7): Error: shadowing declaration test.main.i is deprecated

I think this should be allowable.
It's basically an unfortunate side effect of the decision to make the
loop variables in foreach implicitly 'auto'.
I'd kinda forgotten about that, but I remember when I first saw it
thinking it looked like a particularly odious bit of special casing
just in the name of saving a few keystrokes.  It's the only place in D
where you can declare a variable without any type or storage class
AFAIK.    The only think like it is some of the crazy 'is( )'
expressions where you can define a new type without any 'alias' or
'typedef'.

I wouldn't cry if D2 made specifying 'auto' a requirement in foreach. And allowed using an outer variable as the index like every other loop.  It would certainly be more consistent.

--bb
January 11, 2009
On Sun, Jan 11, 2009 at 12:04 PM, Tim M <a@b.com> wrote:
> On Sun, 11 Jan 2009 15:59:26 +1300, Tim M <a@b.com> wrote:
>> Why does it still work for some objects?
>
>
> This works:
>
>
> module test;
>
> class A
> {
>        this()
>        {
>                //
>        }
> }
>
> class B
> {
>        this()
>        {
>                //
>        }
>        int opApply (int delegate (inout B) dg)
>        {
>                return 1;
>        }
> }
>
> void main()
> {
>        A a;
>        B b;
>        foreach(a; b)
>        {
>                //
>        }
> }

Interesting.  But there the inner 'a' is actually a B.  So it compiles, but there's no way it's using the outer 'a' as the counter variable.

--bb
January 11, 2009
On Sun, 11 Jan 2009 16:10:39 +1300, Bill Baxter <wbaxter@gmail.com> wrote:

> On Sun, Jan 11, 2009 at 12:04 PM, Tim M <a@b.com> wrote:
>> On Sun, 11 Jan 2009 15:59:26 +1300, Tim M <a@b.com> wrote:
>>> Why does it still work for some objects?
>>
>>
>> This works:
>>
>>
>> module test;
>>
>> class A
>> {
>>        this()
>>        {
>>                //
>>        }
>> }
>>
>> class B
>> {
>>        this()
>>        {
>>                //
>>        }
>>        int opApply (int delegate (inout B) dg)
>>        {
>>                return 1;
>>        }
>> }
>>
>> void main()
>> {
>>        A a;
>>        B b;
>>        foreach(a; b)
>>        {
>>                //
>>        }
>> }
>
> Interesting.  But there the inner 'a' is actually a B.  So it
> compiles, but there's no way it's using the outer 'a' as the counter
> variable.
>
> --bb

Sorry for my typo but change that line to:
int opApply (int delegate (inout A) dg)

and it still compiles.

January 11, 2009
On Sun, Jan 11, 2009 at 12:15 PM, Tim M <a@b.com> wrote:
> On Sun, 11 Jan 2009 16:10:39 +1300, Bill Baxter <wbaxter@gmail.com> wrote:
>
>> On Sun, Jan 11, 2009 at 12:04 PM, Tim M <a@b.com> wrote:
>>>
>>> On Sun, 11 Jan 2009 15:59:26 +1300, Tim M <a@b.com> wrote:
>>>>
>>>> Why does it still work for some objects?
>>>
>>>
>>> This works:
>>>
>>>
>>> module test;
>>>
>>> class A
>>> {
>>>       this()
>>>       {
>>>               //
>>>       }
>>> }
>>>
>>> class B
>>> {
>>>       this()
>>>       {
>>>               //
>>>       }
>>>       int opApply (int delegate (inout B) dg)
>>>       {
>>>               return 1;
>>>       }
>>> }
>>>
>>> void main()
>>> {
>>>       A a;
>>>       B b;
>>>       foreach(a; b)
>>>       {
>>>               //
>>>       }
>>> }
>>
>> Interesting.  But there the inner 'a' is actually a B.  So it compiles, but there's no way it's using the outer 'a' as the counter variable.
>>
>> --bb
>
> Sorry for my typo but change that line to:
> int opApply (int delegate (inout A) dg)
>
> and it still compiles.

'Still compiles" or "then it will compile"?
Anyway, I'm curious if it's using the outer A or not.  Can you add a
line or two to actually modify 'a' in the loop?

--bb
January 11, 2009
On Sun, 11 Jan 2009 06:04:01 +0300, Tim M <a@b.com> wrote:

> On Sun, 11 Jan 2009 15:59:26 +1300, Tim M <a@b.com> wrote:
>
>> On Sun, 11 Jan 2009 15:50:54 +1300, Daniel Keep <daniel.keep.lists@gmail.com> wrote:
>>
>>>
>>>
>>> Tim M wrote:
>>>>  Why is this an error. Dmd wants to make sure that I declare a new variable in the foreach statement and not use an existing one?
>>>>  module test;
>>>>  void main()
>>>> {
>>>>     int i;
>>>>     int[] nums;
>>>>     foreach(i; nums)
>>>>     {
>>>>         //
>>>>     }
>>>> }
>>>>   dmd test.d
>>>>  test.d(7): Error: shadowing declaration test.main.i is deprecated
>>>
>>> Yes; as the error states, you're not allowed to define variables with the same name as variables in an enclosing scope any more.
>>>
>>>    -- Daniel
>>
>> Why does it still work for some objects?
>
>
> This works:
>
>
> module test;
>
> class A
> {
> 	this()
> 	{
> 		//
> 	}
> }
>
> class B
> {
> 	this()
> 	{
> 		//
> 	}
>   	int opApply (int delegate (inout B) dg)
>          {
>                  return 1;
>          }
> }
>
> void main()
> {
> 	A a;
> 	B b;
> 	foreach(a; b)
> 	{
> 		//
> 	}
> }
>

It is a bug and should be reported.

« First   ‹ Prev
1 2