Bug 19334 – Derived class inheritance private members in same module

Status
RESOLVED
Resolution
INVALID
Severity
critical
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2018-10-25T22:48:57Z
Last change time
2018-10-26T15:34:56Z
Assigned to
No Owner
Creator
alexanderheistermann

Comments

Comment #0 by alexanderheistermann — 2018-10-25T22:48:57Z
import std.stdio; class A { package int x; private int y; } class B : A { }; void main() { B b = new B(); b.y = 1; // Should be a compile error writeln(b.y); writeln(b.x); } Remove "package int x" and it works as intended.
Comment #1 by alexanderheistermann — 2018-10-25T23:04:04Z
Scratch that, it still accepts invalid code even package int x is removed. Class b shouldn't have property y, as property y only exist at class A not class B. import std.stdio; class A { private int y; } class B : A { }; void main() { B b = new B(); b.y = 1; // Should be a compile error as class shouldn't have private int y. writeln(b.y); }
Comment #2 by aldacron — 2018-10-26T02:24:03Z
This is not a bug and is working as intended: "Symbols with private visibility can only be accessed from within the same module." https://dlang.org/spec/attribute.html#visibility_attributes
Comment #3 by alexanderheistermann — 2018-10-26T14:44:09Z
"This is not a bug and is working as intended" I strongly disagree with that notation.
Comment #4 by aldacron — 2018-10-26T15:14:51Z
The unit of encapsulation in D is the module. If you absolutely positively can't live with access to private members in the same module, then keep your classes in separate modules under the same package namespace.
Comment #5 by alexanderheistermann — 2018-10-26T15:34:56Z
This isn't about encapsulation, this is about inheritance. Class B should NOT inherent Class A private members, irregardless of in the same module or not. Period. If you want me to write a DIP on deprecating this behavior I would gladly do so.