Comment #0 by andrej.mitrovich — 2013-08-20T12:23:31Z
-----
module foo;
struct S
{
int x;
int y;
private:
int _special;
}
-----
-----
module test;
import foo;
void main()
{
auto s = S(1, 2, 3);
}
-----
$ dmd -c test.d
>
The _special field was initialized, even though it's not an accessible field from the 'test' module.
I think this is an oversight that we could fix.
The workaround is to define a custom ctor, although this might be tedious if all that's needed is basic field initialization.
But note that there are edge cases if we try to implement this. For example:
-----
module foo;
struct S
{
int x;
int y;
private int _special;
int z;
}
-----
----
module test;
import foo;
void main()
{
auto s = S(1, 2, 3);
}
----
If we forbid field initialization of private variables, does the above make 'z' equal to 3 (in other words, _special is simply skipped over), or does the compiler simply generate an error stating _special cannot be initialized from the 'test' module?
I think the simplest and safest implementation is to always attempt field initialization in the order of the declarations, without hiding inaccessible fields, so the above code would produce an error.
Comment #1 by robert.schadek — 2024-12-13T18:10:42Z