Re: Foreach problem

2009-01-12 Thread Bill Baxter
On Tue, Jan 13, 2009 at 6:25 AM, Steven Schveighoffer
schvei...@yahoo.com wrote:

 because that's basically what a foreach does when using opApply: create an
 inner function and then pass a delegate pointing to that function to
 opApply.

 I think the difference between the two is that the compiler handles foreach
 on an array in a special manner without using an inner function/delegate.

That's an implementation detail and shouldn't effect the behavior
visible to the user.

--bb


Re: Foreach problem

2009-01-11 Thread Tim M
On Sun, 11 Jan 2009 20:31:51 +1300, Denis Koroskin 2kor...@gmail.com  
wrote:



Yep that probibly is a slight bug. What I would like to know is why  
cant I do foreach with primitive types like I can with objects.


You can use foreach to iterate over arrays and tuples, if that's what  
you mean. Iterating over over primitive types makes little sense.



Just re reading my own question I think it is clear what I meant. I meant  
array of primitives so yes iterating over an array, but an array of  
primitives. Dmd handles foreach over SomeObject[] a lot more relaxed than  
int[].





Foreach problem

2009-01-10 Thread Tim M


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


Re: Foreach problem

2009-01-10 Thread Daniel Keep



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


Re: Foreach problem

2009-01-10 Thread Tim M
On Sun, 11 Jan 2009 15:50:54 +1300, Daniel Keep  
daniel.keep.li...@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?


Re: Foreach problem

2009-01-10 Thread Tim M

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.li...@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)
{
//
}
}



Re: Foreach problem

2009-01-10 Thread Bill Baxter
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


Re: Foreach problem

2009-01-10 Thread Tim M

On Sun, 11 Jan 2009 16:10:39 +1300, Bill Baxter wbax...@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.



Re: Foreach problem

2009-01-10 Thread Bill Baxter
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 wbax...@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


Re: Foreach problem

2009-01-10 Thread Denis Koroskin

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.li...@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.