June 03, 2015 Error: field r must be initialized in constructor, because it is nested struct | ||||
---|---|---|---|---|
| ||||
Pretty standard thing doesn't work and I can't find it on bugzilla: import std.algorithm; struct Foo(R) { R r; this(R r) // <-- Compilation error {} } auto foo(R)(R r) { return Foo!R(r); } void main() { auto arr = [1]; arr.map!(i => i).foo; } I was actually trying to dispatch the construction to a separate function: this(R r) // <-- Still error { init(r); } void init(R r) { this.r = r; } (I remember this latter issue having been discussed recently.) Ali |
June 04, 2015 Re: Error: field r must be initialized in constructor, because it is nested struct | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On Wednesday, 3 June 2015 at 23:27:31 UTC, Ali Çehreli wrote: > Pretty standard thing doesn't work and I can't find it on bugzilla: > > import std.algorithm; > > struct Foo(R) > { > R r; > > this(R r) // <-- Compilation error > {} > } > > auto foo(R)(R r) > { > return Foo!R(r); > } > > void main() > { > auto arr = [1]; > arr.map!(i => i).foo; > } You can shut the compiler up by doing `this.r = R.init;` in the constructor. Searching for "must be initialized" yields issue 13945 which calls for better documentation: https://issues.dlang.org/show_bug.cgi?id=13945 Not covered there is why `map!(i => i)` results in a nested struct. This is the compiler being conservative/stupid, I guess. The lambda could require a context pointer. It doesn't, but the compiler isn't smart enough to see that. Some variants that don't trip the compiler up: arr.map!((int i) => i).foo; static auto identity(T)(T x) {return x;} arr.map!identity.foo; arr.map!"a" It would be nice if the compiler would take this hint, but it doesn't: arr.map!(function (i) => i).foo; |
Copyright © 1999-2021 by the D Language Foundation