Nested struct sauses error
fantom via Digitalmars-d
digitalmars-d at puremagic.com
Sun Mar 12 11:46:14 PDT 2017
I write simple function to get unique values in array of arrays
with position
import std.stdio;
import std.range;
import std.algorithm;
struct Value {
int var;
size_t index;
bool opEquals()(auto ref const Value rhs) const {
return var==rhs.var;
}
int opCmp(ref const Value rhs) const {
return var-rhs.var;
}
}
auto get_unique(int[][] row) {
auto unique=row.enumerate
.map!(iv=>iv.value.map!(var=>Value(var, iv.index)))
.join
.sort()
.group
.filter!(t=>t[1]==1)
.map!(t=>[t[0].var, t[0].index]);
return unique;
}
void main(){
auto
row=[[1,2,3,4,5],[2,3,4,5,6],[3,4,5,6,7],[4,5,6,7,8],[5,6,7,8,9]];
get_unique(row).writeln;
}
Output: [[1, 0], [9, 4]]
But when I move Value struct into function, I get compilation
error
import std.stdio;
import std.range;
import std.algorithm;
auto get_unique(int[][] row) {
struct Value {
int var;
size_t index;
bool opEquals()(auto ref const Value rhs) const {
return var==rhs.var;
}
int opCmp(ref const Value rhs) const {
return var-rhs.var;
}
}
auto unique=row.enumerate
.map!(iv=>iv.value.map!(var=>Value(var, iv.index)))
.join
.sort()
.group //here: Error: template instance
std.algorithm.iteration.group!("a == b", SortedRange!(Value[], "a
< b")) error instantiating
.filter!(t=>t[1]==1)
.map!(t=>[t[0].var, t[0].index]);
return unique;
}
void main(){
auto
row=[[1,2,3,4,5],[2,3,4,5,6],[3,4,5,6,7],[4,5,6,7,8],[5,6,7,8,9]];
get_unique(row).writeln;
}
dmd2/include/std/algorithm/iteration.d(1419): Error: field
_current must be initialized in constructor, because it is nested
struct
app.d(21): Error: template instance
std.algorithm.iteration.group!("a == b", SortedRange!(Value[], "a
< b")) error instantiating
Can anybody explain me what is wrong? Thanks
More information about the Digitalmars-d
mailing list