Bug 7668 – std.algorithm.reduce of array of const ints too
Status
RESOLVED
Resolution
WORKSFORME
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2012-03-07T20:09:00Z
Last change time
2014-02-16T14:28:31Z
Keywords
rejects-valid
Assigned to
nobody
Creator
bearophile_hugs
Comments
Comment #0 by bearophile_hugs — 2012-03-07T20:09:38Z
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 by peter.alexander.au — 2014-01-26T09:01:51Z
This appears to be have been fixed in 2.062 although I can't find the exact pull that fixed it.
Comment #2 by peter.alexander.au — 2014-02-16T14:28:31Z
Changed to WORKSFORME so that it doesn't appear in the changelog for next update.