method returning child, doesn't overrides declared method returning parent
Jonathan M Davis
jmdavisProg at gmx.com
Mon Aug 29 23:56:14 PDT 2011
On Tuesday, August 30, 2011 08:57:53 Mariusz Gliwiński wrote:
> I'm sorry - i provided wrong example. I had property in my code, and he
> didn't complained about getter, but setter (of course he can't diversify by
> different return values).
>
> <code>
> interface Interface
> {
> void method(Interface);
> }
> class Class : Interface
> {
> void method(Class) {}
> }
>
> void main() {}
> </code>
>
> So, You're saying it should be possible, and this is DMD bug? So i'll report
> that.
This particular example is not a bug. A function which returns a Class can be
used in exactly the same situations as one which returns an Interface, because
an instance of Class _is_ an instance of Interface. However, an instance of
Interface is not necessarily an instance of Class. So, if method takes an
instance of Class, it can't take an instance of Interface unless it's also a
Class. So, if you had
Interface i = funcWhichReturnsInterface();
Class c = new Class;
i.method(c); //This will compile.
c.method(i); //This won't compile.
it wouldn't work. Class' implementation of the method function must take
_everything_ that Interface's method function can take, and it doesn't in your
example.
- Jonathan M Davis
More information about the Digitalmars-d
mailing list