method returning child, doesn't overrides declared method returning parent
Jonathan M Davis
jmdavisProg at gmx.com
Tue Aug 30 10:28:22 PDT 2011
On Tuesday, August 30, 2011 10:06 Timon Gehr wrote:
> On 08/30/2011 06:43 PM, Steven Schveighoffer wrote:
> > On Tue, 30 Aug 2011 12:29:59 -0400, Andrei Alexandrescu
> >
> > <SeeWebsiteForEmail at erdani.org> wrote:
> >> On 8/30/11 11:06 AM, Steven Schveighoffer wrote:
> >>> When I write code that derives from a base class, I'm declaring with
> >>> override that I want to implement the base class' function.
> >>> When I write code that implements an interface, I'm declaring with
> >>> override that I want to implement the interface's function.
> >>
> >> From the cycle "deadpan answers": I think one should use "override"
> >> when one wants to override.
> >
> > Then your description of cases where override helps prevent bugs should
> > reflect that:
> >
> > (a) User thinks she overrides a specific method but instead introduces a
> > new one.
> >
> > (b) User thinks she introduces a new method but instead overrides one.
> >
> > I consider implementing an interface method to be hooking, since you are
> > hooking calls from said interface.
> >
> > I guess if we want to avoid solving all hooking problems, even those
> > where one does not intend to implement an interface, but accidentally
> > does, or introduce large annoyances where someone changes a widely used
> > interface to an abstract class, then I guess the status quo is good.
> >
> > -Steve
>
> I don't think that you can change a widely used interface into an
> abstract class and not introduce annoyances much larger than override is
> capable of creating.
>
> How does the status quo prevent implementing interface methods by accident?
If the class doesn't claim that it's implmenting the interface, then it's a
non-issue. You have to declare it to be implementing the interface (or one of
its base classes must be declared to implement it) for it to be usable as that
interface.
If you declare a base class to implement an interface, then that base class
must either delare or implement all of the functions in the interface. Any
function which a derived class might implement is going to need override
(assuming that you compile with -w), since it would be overriding the base
class' function.
If you declare a derived class to implement an interface, then that class must
declare or implement all of the functions in the interface. Any function in a
base class which happens to match an interface function won't count towards
implementing the interface (not even with alias apparently - I don't know if
that's a bug or not). So, it would have to be declared in the derived class,
and (assuming that you compile with -w) override would catch the fact that a
base class happened to implement the same function. But even without override,
you can't use the base class as the interface anyway, so the fact that the
derived class must declare or implement all of the interface's functions
itself means that a base class implementation won't accidentally count as
implementing an interface function.
The _only_ case where you can accidentally implement an interface function is
if you have a function in the class which is supposed to be implementing the
interface which happens to implement an interface function, and you didn't
notice - that is, you declared the function previously, added the fact that
the class implements an interface, forgot to make the class properly implement
one of the interface's functions, the existing function matches the interface
function that you missed, and you don't notice. That's not a particularly
likely scenario unless you have ridiculously large class or are doing
something with mixins.
So, "accidentally" implementing an interface function isn't really an issue.
- Jonathan M Davis
More information about the Digitalmars-d
mailing list