[Issue 20358] External initialization of private struct fields should be disallowed
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Thu Nov 7 15:38:36 UTC 2019
https://issues.dlang.org/show_bug.cgi?id=20358
--- Comment #7 from Max Samukha <maxsamukha at gmail.com> ---
(In reply to Simen Kjaeraas from comment #6)
> In favor of closing as invalid: If your type has an invariant that requires
> private fields to have specific values, you should define a constructor that
> establishes said invariant. If you don't, you've essentially told the world
> you'll accept any and all values.
But there is also lazy initialization:
// invariant is x == 1.
// neither disabled default ctor nor non-default ctors are wanted
struct S {
private int x;
void foo() {
if (x == 0) x = 1;
}
}
auto s = S(2); // problem
I agree that the issue is probably minor, because most structs with private
fields will have at least one constructor defined or disabled anyway. I
disagree that the issue is invalid.
--
More information about the Digitalmars-d-bugs
mailing list