[Issue 20358] External initialization of private struct fields should be disallowed

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Nov 7 09:57:42 UTC 2019


https://issues.dlang.org/show_bug.cgi?id=20358

--- Comment #6 from Simen Kjaeraas <simen.kjaras at gmail.com> ---
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.

If we were designing D from the start, I'd go with the private constructors I
described above*. As it is, changing this would break code for relatively small
benefit.

Even if default constructors are thought of the way Basile indicates in comment
#4, that's no excuse for S s = {x: 1};, though - that should be disallowed.


* Curly bracket initialization could be handled separately by respecting field
visibility rules, or simply disallowed - it's a blunt tool for simple types,
and more complex types with invariants can simply define a constructor. Yes,
there are valid use cases, but they can easily be handled by explicitly defined
constructors.

--


More information about the Digitalmars-d-bugs mailing list