On 06/27/2015 10:43 AM, aki wrote:

> void pr(Array!int a) {
>      foreach(i, v; a[]) {

You are rightly assuming that the loop counter is available for all container types. Unfortunately, it is the case only for arrays.

Luckily, it is very easy to achieve it with std.algorithm.enumerate:

import std.stdio;
import std.container;
import std.range;

void pr(Array!int a) {
    foreach(i, v; a[].enumerate) {
        writeln("%4s: %s\n", i, v);
    }
}

void main()
{
    auto a = Array!int();
    pr(a);
}

Ali

Reply via email to