Comment #0 by peter.alexander.au — 2015-01-06T21:59:51Z
Some example code:
----------------------
struct A(T) {
T x;
this(int) {} // Error: field x must be initialized in constructor, because it is nested struct
}
void bar(T)() {
T y; // Error: cannot access frame pointer of foo.main.X
}
void main() {
struct X {
this(int) {}
}
bar!X();
A!X(1);
}
----------------------
I can find no explanation of why these errors would occur in the documentation.
Furthermore, the errors are uninformative. Ok, so T is a nested struct. Why do I have to initialize it in the constructor? What is a "frame pointer" and why does it need to access it to construct the object? (frame pointer is an implementation detail, and such terminology should not be exposed to the user).
To resolve this bug, I'd like to see two things:
1) Documentation of *why* nested structs need special initialization, and precise definition of what counts as initialization. I know there are limitation with using loops, and calling other functions, but this is documented no where as far as I can see.
2) Improved error message. For these, I would prefer something along the lines of:
"Error: field x must be initialized directly in constructor from another instance of T, since it is a nested struct and requires its outer context"
"Error: y must be constructed from another instance of T, since it is a nested struct and requires context"
These are just ideas.
Comment #1 by acehreli — 2015-06-04T06:29:40Z
Found another example that does not involve a nested struct:
import std.algorithm;
struct Foo(R)
{
R r;
this(R r) // <-- Compilation error
{}
}
auto foo(R)(R r)
{
return Foo!R(r);
}
void main()
{
[1].map!(i => i).foo;
}
Error: field r must be initialized in constructor, because it is nested struct
Ali
Comment #2 by robert.schadek — 2024-12-15T15:22:41Z