contravariant argument types: wanna?
Andrei Alexandrescu
SeeWebsiteForEmail at erdani.org
Thu Sep 24 06:06:41 PDT 2009
Steven Schveighoffer wrote:
> On Wed, 23 Sep 2009 11:13:26 -0400, Andrei Alexandrescu
> <SeeWebsiteForEmail at erdani.org> wrote:
>> class AutoVehicle { ... }
>> class Car : AutoVehicle { ... }
>> class Truck : AutoVehicle { ... }
>>
>> class Driver {
>> // A driver is licensed to drive a car
>> void drive(Car c);
>> }
>>
>> class TruckDriver : Driver {
>> // A truck driver is licensed to drive a car...
>> override void drive(Car c);
>> // ... and a truck
>> void drive(Truck c);
>> // No contravariance needed yet
>> }
>>
>> class JamesBond : Driver {
>> // James Bond can drive any auto vehicle
>> // Contravariance needed here
>> override void drive(AutoVehicle c) { ... }
>> }
>>
>> Now if what you have is a JamesBond and a Truck, you need
>> contravariance to have him drive it. (A HotGirl may or may not be
>> present in the scene.)
>
> Your example just triggered a possible problem with contravariance.
> Consider this class:
>
> class Bad : TruckDriver {
> override void drive(AutoVehicle c) { ...}
> }
>
> What does this override, drive(Truck) or drive(Car), or both? What if
> you didn't want to override both? My instinct is that this should be an
> error, to keep things simple. But it might be very annoying for some
> designs...
It should override both. It is already the case that one method
overrides several others (e.g. with multiple inheritance of interfaces).
Andrei
More information about the Digitalmars-d
mailing list