forbid field name conflict in class hierarchy

spir denis.spir at
Sun Nov 14 10:46:08 PST 2010


I think the compiler should complain when sub-classes hold fields with the same name as super-classes. After all, names (in addition to types) are used to identify. Intentionally reusing the same name would not only be bad design, but open the door to hidden bugs.
Remain unintentional name crash: eg forgot to remove a field when established type hierarchy. Allowing same names lead to strange contradictions by the language -- see below. Without such a rigor imposed the compiler, we can easily fall into traps such as:

class C {
    int i;
    this (int i) {
        this.i = i;
class C1 : C {
    // forgot to remove i
    int i;
    int j;
    this (int i, int j) {
        super(i);	// think i is set?
        this.j = j;
void main () {
    auto c1 = new C1(1,2);
    writeln(c1.i);  // 0

Got me 3 times already. I don't understand how it is even possible that C.i is not the same as C1.i, but evidence is here... There is a contradiction: i is set && not set. (explaination welcome ;-)

-- -- -- -- -- -- --
vit esse estrany ☣

More information about the Digitalmars-d mailing list