foreach ref very broken: fails to call front(val)

Ali Çehreli acehreli at yahoo.com
Tue Jul 10 00:25:15 PDT 2012


On 07/09/2012 02:24 AM, monarch_dodra wrote:
 > On Monday, 2 July 2012 at 12:44:59 UTC, monarch_dodra wrote:
 >> ...
 >
 > I opened this thread a week ago, and got little no feed back.

Please also consider the D.learn newsgroup. Some people are more 
responsive over there. :) (And you may have already asked there anyway. :))

 > import std.stdio;
 > import std.algorithm;
 > import std.container;
 >
 > void main()
 > {
 > Array!int arr = Array!int(1, 2, 3, 4, 5);
 > foreach(ref a; arr[])
 > {
 > a = 10;
 > }
 > writeln("Output: ", arr[]);
 > }
 > ----
 > Output: [1, 2, 3, 4, 5]
 > ----

Yet using the InputRange functions directly works:

     Array!int arr = Array!int(1, 2, 3, 4, 5);
     auto all = arr[];

     for ( ; !all.empty; all.popFront()) {
         all.front = 10;
     }

Now, the output: [10, 10, 10, 10, 10]

Using for and indexes also works:

     Array!int arr = Array!int(1, 2, 3, 4, 5);
     auto all = arr[];

     for (auto i = 0; i != all.length; ++i) {
         all[i] = 10;
     }

Again, the output: [10, 10, 10, 10, 10]

 > I realize I'm still new to D, but I'd appreciate if somebody told me if
 > I am wrong(or being stupid).

I have found std.container to be confusing.

 > Anyways, I'm bothered by several things:
 >
 > Quoteth "The D programing language", 12.9.1: "If you specify ref with
 > value, the compiler replaces all uses of value with calls to __c.front
 > throughout the body of the loop".
 >
 > Surely, there is something wrong here... right?

Yes. :)

Ali



More information about the Digitalmars-d mailing list