contravariant argument types: wanna?

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Tue Sep 22 19:35:36 PDT 2009


Marianne Gagnon wrote:
> I'm admittedly not a D expert, and have not written compilers; but the first thing that came to my mind is that the second fun() you posted was not an override, but overload. i.e. the same as the following, except with inheritance :
> 
> print(int a) { /* ... */ }
> print(float a) { /* ... */ }
> print(string a) { /* ... */ }

Inheritance makes all the difference. Consider:

B b = new B;
A a = b;
auto c1 = b.clone();
auto c2 = a.clone();

Both calls go to the same method, although c1 and c2 have different types.

> Now the main question, IMO, is : how will the compiler differenciate whether you want to override or overload? Would that change prevent overloading methods with different signatures?

The rule is simple - for overriding to happen, you must (1) use the 
override keyword, (2) have the result type a subtype of the result type 
of the overridden method, and (3) have the parameter types supertypes of 
the respective parameter types of the overriden method.


Andrei



More information about the Digitalmars-d mailing list