Andrei Alexandrescu wrote:
Walter Bright wrote:
Andrei Alexandrescu wrote:
Today D does not support contravariant arguments, but Walter told me
once he'd be quite willing to implement them. It is definitely the
right thing to do, but Walter would want to see a compelling example
before getting to work.
The stumbling block to contravariant parameters (not arguments) is how
it throws a monkey wrench into the overloading rules. The problem is
deciding if a function overrides or overloads, when its parameter
types are different.
The current rule is simple: if the parameter types are identical, it
overrides. If they are not identical, it overloads.
I don't see that as a huge problem. First off, right now the override
keyword is required (and for good reason, I might add).
That is a good point.
Second, the new
rule is simple: if the overriding function can be called with the
overriden function's arguments, it is overriding it. True, things get
more complicated when the base class also defines a corresponding overload:
class A {
void fun(A);
void fun(B);
}
class B : A {
override void fun(A);
}
This must be either an error, or the case that B.fun overrides both
overloads of fun in A.
I would really want to get away from the notion of selecting which
function is overridden based on being a "better" match.