Why does enumerate over range return dchar, when ranging without returns char?

James Blachly james.blachly at gmail.com
Thu May 3 05:44:51 UTC 2018


I am puzzled why enumerating in a foreach returns a dchar (which 
forces me to cast), whereas without the enumerate the range 
returns a char as expected.

Example:

```
import std.stdio;
import std.range : enumerate;

void main()
{
	char[] s = ['a','b','c'];

     char[3] x;
     auto i = 0;
     foreach(c; s) {
         x[i] = c;
         i++;
     }

     writeln(x);
}
```
Above works without cast.

'''
import std.stdio;
import std.range : enumerate;

void main()
     {
	char[] s = ['a','b','c'];

     char[3] x;
     foreach(i, c; enumerate(s)) {
         x[i] = c;
         i++;
     }

     writeln(x);
}
```
Above fails without casting c to type char.

The function signature for enumerate shows "auto" return type, so 
that does not help me understand.

Kind regards


More information about the Digitalmars-d-learn mailing list