Error: field r must be initialized in constructor, because it is nested struct
anonymous via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Jun 3 18:13:05 PDT 2015
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;
More information about the Digitalmars-d-learn
mailing list