Why is std.algorithm.reduce impure?

bearophile bearophileHUGS at lycos.com
Tue Mar 6 16:49:37 PST 2012


H. S. Teoh:

> But why can't 'this' be const? For example, why does the compiler reject
> this:
> 
> 	class A {
> 		int[] data;
> 		pure const int sum() {
> 			return reduce!"a*b"(data);
> 		}
> 	}
> 
> I'm not modifying data at at all, so why should it be an error?

I think it's a small bug in std.algorithm.reduce, is this in Bugzilla already?


import std.algorithm: reduce;
void main() {
    const data = [2, 3, 4];
    int r1 = reduce!q{a * b}(0, data); // OK
    int r2 = reduce!q{a * b}(data);
}



In std.algorithm.reduce there is also this (now bug 2443 is fixed) at about line 723:

            // For now, just iterate using ref to avoid unnecessary copying.
            // When Bug 2443 is fixed, this may need to change.
            foreach(ref elem; r)
            {
                if(initialized)

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list