D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 7668 - std.algorithm.reduce of array of const ints too
Summary: std.algorithm.reduce of array of const ints too
Status: RESOLVED WORKSFORME
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: x86 Windows
: P2 normal
Assignee: No Owner
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2012-03-07 20:09 UTC by bearophile_hugs
Modified: 2014-02-16 14:28 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2012-03-07 20:09:38 UTC
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); // error
}


DMD 2.059head gives:

...\dmd2\src\phobos\std\algorithm.d(731): Error: can only initialize const member _field_field_0 inside constructor
...\dmd2\src\phobos\std\conv.d(3334): Error: *chunk is not mutable
...\dmd2\src\phobos\std\algorithm.d(743): Error: template instance std.conv.emplace!(const(int),const(int)) error instantiating
test.d(5):        instantiated from here: reduce!(const(int[]))
test.d(5): Error: template instance std.algorithm.reduce!("a * b").reduce!(const(int[])) error instantiating


I think  reduce!q{a*b}(data)  too should compile and work.

--------------

Extra note: 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)
Comment 1 Peter Alexander 2014-01-26 09:01:51 UTC
This appears to be have been fixed in 2.062 although I can't find the exact pull that fixed it.
Comment 2 Peter Alexander 2014-02-16 14:28:31 UTC
Changed to WORKSFORME so that it doesn't appear in the changelog for next update.