Comment #0 by bearophile_hugs — 2014-12-01T16:42:09Z
From issue 13595. This looks like a compiler bug:
void main() {
import std.algorithm: map, groupBy;
//[""].map!((string s) => s).groupBy!((x, y) => true); // OK
[""].map!((s) => s).groupBy!((x, y) => true); // Error
}
dmd 2.067alpha gives:
...\dmd2\windows\bin\..\..\src\phobos\std\algorithm.d(4659): Error: field _prev must be initialized in constructor, because it is nested struct
...\dmd2\windows\bin\..\..\src\phobos\std\algorithm.d(4770): Error: template instance temp.main.groupBy!(__lambda2, cast(Flag)false, MapResult!(__lambda1, string[])) error instantiating
temp.d(5): instantiated from here: groupBy!((x, y) => true, MapResult!(__lambda1, string[]))
The error doesn't appear if you replace "(s)" with "(string s)".
Comment #1 by k.hara.pg — 2014-12-01T17:17:07Z
This would be a Phobos bug in GroupByImpl struct.
static if (isForwardRange!Range)
{
private Range _prev;
private void savePrev() { _prev = r.save; }
private @property ElementType!Range prev() { return _prev.front; }
}
else
{
private ElementType!Range _prev;
private void savePrev() { _prev = r.front; }
private alias prev = _prev;
}
this(Range _r)
{
r = _r;
if (!empty)
{
// Check reflexivity if predicate is claimed to be an equivalence
// relation.
assert(!equivRelation || pred(r.front, r.front),
"predicate " ~ pred.stringof ~ " is claimed to be "~
"equivalence relation yet isn't reflexive");
savePrev(); // <---- here
}
}
As error message says, the _prev field is not correctly initialized inside constructor. Indeed, it's assigned in savePrev member function, but compiler cannot handle the assignment as the initialization.
Comment #2 by peter.alexander.au — 2015-01-02T00:45:17Z