Why is std.algorithm.reduce impure?

Jonathan M Davis jmdavisProg at gmx.com
Tue Mar 6 16:41:22 PST 2012


On Tuesday, March 06, 2012 14:41:01 H. S. Teoh wrote:
> Why is std.algorithm.reduce not marked pure? It makes it impossible to
> do things like this:
> 
> pure const int product(int[] args) {
> return reduce!"a * b"(args);
> }

You'd have to look through the implementation and possibly tweak it to figure 
it out. All it takes is _one_ thing in there which isn't considered pure, and 
reduce won't be pure.

Glancing at it, I suspect that its use of emplace is the culprit. emplace uses 
memcpy in some of its overloads, and memcpy probably isn't marked as pure, 
since it's a C function. It theoretically _could_ be marked as pure - and 
arguably should be - but I very much doubt that it is right now.

It really takes very little for something to be impure, and optimizations 
often do it, because they end up using low-level constructs which aren't pure 
- some of which could be but aren't and others which probably can't be. More 
effort needs to be made in sorting out the purity of a number of the low-level 
constructs. Appender, for instance, screws up a lot of the string stuff, 
because it isn't pure. And it's far from the sole culprit.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list