[dmd-internals] Method overriding allows covariant conversions (for 2.059 release)

kenji hara k.hara.pg at gmail.com
Sat Mar 3 05:44:25 PST 2012


It is useful improvement of 2.059, but there are some issues still.

#1
If one method overrides two base methods with covariance but no exact
overriding...

class C {
    int foo() { return 1; }           // (1)
    int foo() immutable { return 2; } // (2)
}
class D : C {
    override int foo() const { return 3; }
}

In this case, D.foo overrides C.foo (1) and (2) at the same time?
Or it should raise "ambiguous overriding" error?


#2
If each overridings has ambiguity but whole has no ambiguity...

class C {
    int foo()           { return 1; } // (1)
    int foo() immutable { return 2; } // (2)
}
class D : C {
    override int foo()        const { return 3; }  // (3)
    override int foo() shared const { return 4; }  // (4)
}

(3) can override (1) and (2), this is ambiguous.
But, (4) can override only (2).
Then, (3) overrides (1) only and it is disambiguate, no?

#3
(This is my opinion based on an idea suggested by Timon:
http://d.puremagic.com/issues/show_bug.cgi?id=7534#c2 )
If you want to override base class method, we MUST add 'override' keyword.
In other words, no 'override' keyword never overrides anything.

class C {
    int foo(){ return 1;}
}
class D1 : C {
    alias C.foo foo;              // inherit from super class
    int foo() const { return 2; } // additional overload, because no
'override' keyword
}
class D2 : C {
    alias C.foo foo;                       // inherit from super class
    ovreride int foo() const { return 2; } // overrides C.foo, because
'override' keyword added
}
void main()
{
	C mc = new C();
	assert(mc.foo() == 1);

	D2 md1 = new D1();	 assert(md1.foo() == 1);
	const(D1) cd1 = md1; assert(cd1.foo() == 2);
	mc = md1;            assert(mc.foo() == 1);

	D2 md2 = new D1();	 assert(md2.foo() == 2);
	const(D2) cd2 = md2; assert(cd2.foo() == 2);
	mc = md2;            assert(mc.foo() == 2);
}

I think this is appropreate and necessary fix to avoid restriction.

About 'override' keyword requirement, a pull is already suggested by yebblies:
https://github.com/D-Programming-Language/dmd/pull/462
We shouhd merge this pull until 2.059 release.


Thanks.

Kenji Hara


More information about the dmd-internals mailing list