www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Different loops bug

reply "bearophile" <bearophileHUGS lycos.com> writes:
This is derived from part of a D.learn post, maybe it's of your 
interest.

This program shows two similar versions of the same loop:


import std.stdio, std.algorithm, std.typetuple, std.range;

void main() {
     auto items = [[10, 20], [30]];

     foreach (_; 0 .. 2) {
         foreach (sub; items) {
             iota(sub.length)
             .map!((i){ writeln(sub); return 0; })
             .array();
         }
     }

     writeln();

     foreach (_; TypeTuple!(0, 1)) {
         foreach (sub; items) {
             iota(sub.length)
             .map!((i){ writeln(sub); return 0; })
             .array();
         }
     }
}


Its output:

[10, 20]
[10, 20]
[30]
[10, 20]
[10, 20]
[30]

[10, 20]
[10, 20]
[30]
[30]
[30]
[30]


Do you see why the output of the two foreach(_) is different?

Bye,
bearophile
Mar 01 2013
parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 03/01/2013 11:07 PM, bearophile wrote:
 ...

 Do you see why the output of the two foreach(_) is different?
They are not different.
Mar 01 2013
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Timon Gehr:

 They are not different.
Really? Sorry, I don't understand. Bye, bearophile
Mar 01 2013
parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 03/02/2013 12:23 AM, bearophile wrote:
 Timon Gehr:

 They are not different.
Really? Sorry, I don't understand. Bye, bearophile
According to the spec the two outputs are the same.
Mar 01 2013
next sibling parent reply "Brad Anderson" <eco gnuk.net> writes:
On Friday, 1 March 2013 at 23:25:31 UTC, Timon Gehr wrote:
 On 03/02/2013 12:23 AM, bearophile wrote:
 Timon Gehr:

 They are not different.
Really? Sorry, I don't understand. Bye, bearophile
According to the spec the two outputs are the same.
Are you saying they should output the same or that they are the same? You and I have a different definition of "same" if the latter.
Mar 01 2013
parent Timon Gehr <timon.gehr gmx.ch> writes:
On 03/02/2013 01:17 AM, Brad Anderson wrote:
 On Friday, 1 March 2013 at 23:25:31 UTC, Timon Gehr wrote:
 On 03/02/2013 12:23 AM, bearophile wrote:
 Timon Gehr:

 They are not different.
Really? Sorry, I don't understand. Bye, bearophile
According to the spec the two outputs are the same.
Are you saying they should output the same or that they are the same? You and I have a different definition of "same" if the latter.
With a correct implementation of the language, the outputs are the same. Bugs should go to the bug tracker.
Mar 01 2013
prev sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Timon Gehr:

 According to the spec the two outputs are the same.
I have filed this: http://d.puremagic.com/issues/show_bug.cgi?id=9628 Bye, bearophile
Mar 01 2013