D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 5187 - Attribute hiding error or warning
Summary: Attribute hiding error or warning
Status: RESOLVED WONTFIX
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 enhancement
Assignee: No Owner
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2010-11-08 04:03 UTC by bearophile_hugs
Modified: 2022-11-29 10:29 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2010-11-08 04:03:26 UTC
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 Andrej Mitrovic 2014-04-28 11:58:32 UTC
This should probably be in some DIP, or you could ask about it in the forums. It's worth discussing IMO.
Comment 2 bearophile_hugs 2014-04-28 19:57:59 UTC
(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 RazvanN 2022-11-29 10:29:04 UTC
This is documented behavior [1]. There is no need to issue a warning for this.

[1] https://dlang.org/spec/class.html#fields