access foreach index counter for Iterate n times

mw mingwu at gmail.com
Sat May 21 15:06:31 UTC 2022


On Saturday, 21 May 2022 at 11:59:42 UTC, Adam D Ruppe wrote:
> On Saturday, 21 May 2022 at 03:01:39 UTC, mw wrote:
>> Yes! let's call it "loop counter"
>
> It isn't a loop counter. It is an index back into the original 
> source. Consider:
>
> foreach(INDEX, dchar character; "“”")
>    writeln(INDEX); // 0 then 3

Wow, thus surprised me again!

1) First, why not 0 then 4? Since dchar is 32 bits.

2) Second, compare:

import std;
void main()
{
     dstring ds = "“”";
     writeln(ds.length);
     foreach(INDEX, dchar character; ds)
       writeln(INDEX, character); // 0 then
}

Output:
2
0“
1”

Explanations?


If it's "loop counter", isn't the behavior more consistent?



More information about the Digitalmars-d mailing list