D interface bug?

destructionator at gmail.com destructionator at gmail.com
Fri Mar 29 23:52:25 UTC 2019


On Friday, 29 March 2019 at 23:44:35 UTC, Alex wrote:
> interface iBase
> {
> 	iBase fooBase(iBase);
> }
>
>
> class cBase : iBase
> {
> 	cBase fooBase(cBase c) { return c; }	
>
> }
>
> cBase.fooBase should be a valid override of iBase.fooBase 
> because
>  they are the same type!

The return value there is allowed, but the parameter is not. Consider this case:

class AnotherChild : iBase { /* snip */ }

iBase i = new cBase(); // allowed, cBase implements iBase
i.fooBase(new AnotherChild);


That second line should be allowed: the interface says it accepts any implementation of the iBase interface.

But the cBase implementation doen't allow *any* implementation of iBase - it only accepts cBase and down. That function wouldn't be able to handle my AnotherChild instance.

Thus, cBase.fooBase does NOT implement the iBase's interface, and it fails to compile.



More information about the Digitalmars-d-learn mailing list