D-Programming-Language/phobos — pull request #1060 by monarchdodra (2013-01-08 16:22:10+00:00)
Title: Fix const qualified ranges with map/filter

From: http://forum.dlang.org/thread/cvkqglagiamoxnjfyjsl@forum.dlang.org

Basically, both map and filter accepted const ranges, making the simplistic assumption that a range's constness can be stripped by copy. This is absolutely 100% false.

Currently, this has a 50/50 chance of failing to compile with a strange error, or simply lucking-out and working. Even then, when it does work, it needlessly instantiates a `Result!(const R)` that is exactly the same as a `Result!R`. Why even bother making it look like const if it isn't const at all?

These range adapters, IMO, simply should not make any promises to return anything that is mutable if you don't give them something mutable.

That said, why not return something immutable instead? After all, that's what the user is asking for, isn't it?

--------
Map: When calling `map!(const R)(const R r)`, imo, the only logical result is to obtain a `const MapResult!R`.

This works quite well, with *all* types of ranges. The reason we can return a `const MapResult!R` (as opposed to a `const MapResult!(const R)`) is because it is built *as* a const/immutable. This means the object stored inside the `const MapResult!R` *is* a `const R`.

I think it makes perfect sense:
```
map(range) => Map!Range
map(const range) => const Map!Range
map(immutable range) => immutable Map!Range
```

--------
Filter: For filter, I removed any and all support for const ranges. filter simply cannot work with immutable ranges, as the constructor requires mutation. Under this circumstance, there is really nothing we can offer.