import std.algorithm;
import std.stdio;
import std.conv;

class Trivial
{
    int sideEffect() { return n++; }
    override string toString() pure { return to!string(n); }
    int n;
}

void main()
{
    Trivial[] objs = [ new Trivial ];
    map!(o => o.sideEffect())(objs);
    writeln(objs);      // [0]
    foreach (o; objs) o.sideEffect();
    writeln(objs);      // [1]
}

Can someone explain to me why map() is not equivalent to foreach in the code above? From what I can tell, map() doesn't do anything at all on objs, even though it is a perfectly legitimate range (as far as I can tell).

Dave

Reply via email to