Attribute hiding, strict compiler

bearophile bearophileHUGS at lycos.com
Mon Nov 8 04:27:43 PST 2010


I have just added a issue about class/struct attribute hiding:
http://d.puremagic.com/issues/show_bug.cgi?id=5187

In C# this code generates a warning:

public class Foo {
    public int x = 10;
}
public class Test : Foo {
    public int x = 20; // warning
    public static void Main() {}
}


warning CS0108: `Test.x' hides inherited member `Foo.x'. Use the new keyword if
hiding was intended

While this C# code gives no warning, as you see it uses the "new" keyword:


public class Foo {
    public int x = 10;
}
public class Test : Foo {
    new public int x = 20; // OK
    public static void Main() {}
}

--------------------

That's an example of a significant general difference I have seen from the C# and D compilers. Compared to DMD the C# (Mono) compiler is *much* more strict and tidy. Compared to it DMD feels quite sloppy. So I am asking Walter if this is something temporary, that will change, or it is something meant to be. Here I am especially thinking about my wide issue 3934:

http://d.puremagic.com/issues/show_bug.cgi?id=3934

DMD 2.050 compiles the following horror program with no errors or warnings:


auto scope shared import std.stdio;
static foo1() {}
final foo2() {}
ref foo3() {}
enum void foo4() {}
nothrow foo5() {}
pure foo6() {}
extern struct foo7;
struct Foo8 { static invariant() {} }
__gshared struct foo9 { int n; }
struct Foo10 { protected int x; }
void foo11() { static enum x = 10; }
private class Foo12 {}
public class Foo13 : Foo12 {}
static int x1 = 10;
static x2 = 10;
class Base {
    private final ~this() { writeln("Base.~this"); }
}
class Derived : Base {
    private final ~this() { writeln("Derived.~this"); }
}
class A {
    @disable override equals_t opEquals(Object other) { return false; }
}
auto void main() {
    new Derived();
    auto a = new A();
    auto b = new A();
    if (a == b)
        assert(0);
}


If you try to compile something vaguely similar in C# the compiler spits in your eye in a fraction of second.
So is DMD supposed to become (much) more strict?

Bye,
bearophile


More information about the Digitalmars-d mailing list