s1s0 / toped

Cross platform, open source IC layout editor
http://www.toped.org.uk/
GNU General Public License v2.0
15 stars 8 forks source link

"foreach" unappropriate behaviour #141

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
The way this operation is implemented is not actually the expected behaviour. 
Here is the example

int list a = {0,1,2,3,4,5};
foreach(int i; a)
   i = i+1;

After the execution of the above one would expect that the value of "a" would 
be {1,2,3,4,5,6}, but it isn't. The value remains unchanged. Looking at the 
parser it appears that the operation works with a copy of the list, not 
directly over it. And there is a very good reason for it in the parser 
structure. On the surface it can be demonstrated with the examples:

foreach(int i; (int list) {0,1,2,3,4,5}) {...} //  anonymous variables
foreach(int i; seq(1,100)) {...} // function returns

Original issue reported on code.google.com by krustev....@gmail.com on 15 Apr 2012 at 6:28

GoogleCodeExporter commented 9 years ago
Issue found as a consequence of the tests & updates related to Issue 135

Original comment by krustev....@gmail.com on 15 Apr 2012 at 10:27

GoogleCodeExporter commented 9 years ago

Original comment by krustev....@gmail.com on 8 May 2012 at 11:10

GoogleCodeExporter commented 9 years ago
The issue is fixed in r2150 and r2151
Now in case a list variable stays as a second argument of foreach - it will 
pick-up all changes that the loop iterator gets. 
Here is an example:
int list a = {0,1,2,3,4,5};
foreach(int i; a)
   i = i+1;
echo(a);
<=  list members: { 1 , 2 , 3 , 4 , 5 , 6 }

As a bonus - foreach does not require typed anonymous variables anymore (they 
are still acceptable for compatibility) - i.e. now the expression
foreach(int i; {0,1,2,3,4,5}) printf("%d ",i); printf("\n");
//will happily produce
0 1 2 3 4 5 

Original comment by krustev....@gmail.com on 9 May 2012 at 12:06

GoogleCodeExporter commented 9 years ago

Original comment by krustev....@gmail.com on 12 May 2012 at 1:30