Bug 5187 – Attribute hiding error or warning

Status
RESOLVED
Resolution
WONTFIX
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2010-11-08T04:03:26Z
Last change time
2022-11-29T10:29:04Z
Keywords
diagnostic
Assigned to
No Owner
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2010-11-08T04:03:26Z
This D code gives no compilation errors with DMD 2.050: public class Foo { public int x = 10; } public class Test : Foo { public int x = 20; } void main() {} This is equivalent C# code: public class Foo { public int x = 10; } public class Test : Foo { public int x = 20; // warning public static void Main() {} } The C# compiler shows this warning: warning CS0108: `Test.x' hides inherited member `Foo.x'. Use the new keyword if hiding was intended The following C# code gives no warnings: public class Foo { public int x = 10; } public class Test : Foo { new public int x = 20; // OK public static void Main() {} } A similar error (or warning), with a similar "new" keyword solution, may be added to D, so unwanted hiding of attributes may be avoided. This is related to using the obligatory "override" keyword: bug 3836 And this warning that I think is better to become an error ASAP: bug 4216
Comment #1 by andrej.mitrovich — 2014-04-28T11:58:32Z
This should probably be in some DIP, or you could ask about it in the forums. It's worth discussing IMO.
Comment #2 by bearophile_hugs — 2014-04-28T19:57:59Z
(In reply to Andrej Mitrovic from comment #1) > This should probably be in some DIP, or you could ask about it in the > forums. It's worth discussing IMO. In the D design there are thousands of corner cases that weren't kept into account during the D design, because of reasons. Here I am asking for a warning/error, and for a new usage of "new" (or another semantically equivalent syntax), this is both a breaking change, the introduction of a warning (and a syntax change): things currently seriously frowned upon. Even improvements that lot of people seem to like, like the removal of implicit concatenation of adjacent string literals, or a dangerous and bad corner case: https://github.com/D-Programming-Language/dmd/pull/2887 were refused with insufficient reasons), so are such wars still worth fighting for?
Comment #3 by razvan.nitu1305 — 2022-11-29T10:29:04Z
This is documented behavior [1]. There is no need to issue a warning for this. [1] https://dlang.org/spec/class.html#fields