What are virtual functions?

FeepingCreature feepingcreature at gmail.com
Mon Apr 19 06:15:38 UTC 2021


On Sunday, 18 April 2021 at 23:04:26 UTC, ShadoLight wrote:
> On Wednesday, 14 April 2021 at 14:06:18 UTC, FeepingCreature 
> wrote:
>> On Wednesday, 14 April 2021 at 13:43:20 UTC, Berni44 wrote:
> [..]
>> Covariance is related ...
> [..]
>>
>> The opposite (contravariance) happens ...
> [..]
>>
>
> Nice answer but, just to be clear - D only supports covariance 
> on return types at the moment, and doesn't support 
> contravariance on parameters, right?
>
> I remember contravariance being periodically requested in the 
> past but, AFAICR, it has not been implemented, right? A quick 
> search through the forums didn't turn anything up either... and 
> this does not compile:
>
> ```
> class A {}
> class B : A {}
>
> class Y {
> 	public void bar (B b) {}
> }
> class X : Y {
> 	public override void bar (A a){}
> }
> ```

... That doesn't work?! Holy hell. I mean, run.dlang.io confirms, 
but ... *why not*?! If you already support return type 
covariance, parameter contravariance should be easy. It's the 
same thing! You don't need to do anything! Any B is directly a 
valid A! Like, sure, there's versions of this that don't 
trivially work, like mixing interface and class parents, but 
direct superclass contravariance should be easy. ... Weird. I 
don't get it.

Reading https://issues.dlang.org/show_bug.cgi?id=3075 seems like 
it collides with overloading. Except ... it doesn't. Because you 
can't implement multiple overloaded methods with contravariance 
in a subclass, because it doesn't work for interfaces anyway 
(cause they occupy a different interface slot in the class and so 
aren't strictly Liskov.) So the selection of the function to 
override is still unambiguous.

I don't get it.


More information about the Digitalmars-d-learn mailing list