Map Lambda with Side-Effects

"Nordlöw" via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jan 12 23:35:53 PST 2015


Somewhat related to

https://github.com/D-Programming-Language/phobos/pull/2024

I wonder about the soundness of `map` in


```D
import std.algorithm, std.range, std.stdio;
void main(string[] args)
{
       long[] arr;
       const n = 3;
       iota(n).map!(a => arr ~= a);
       writeln(arr);
       writeln(iota(n).map!(a => arr ~= a));
       writeln(arr);
}
```

that prints

```D
[]
[[0], [0, 1], [0, 1, 2]]
[0, 1, 2]
```

Shouldn't a warning at least be issued for return-ignoring calls
to map with mutating lambdas? Has there been any discussions on
making map require pure functions now that we have each? I guess
a new function, say `mapPure`, may be neccessary as adding such a
restriction to the lambda will break too much code right?


More information about the Digitalmars-d-learn mailing list