www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Override Safety

reply =?ISO-8859-1?Q?J=F6rg_R=FCppel?= <joerg sharky-x.de> writes:
Is there a way in D to make sure that interface methods are correctly
overridden in subclasses?


class ListenerIFace
{
  void somethingChanged(int data, int data2);
};


class ChangeListener : public ListenerIFace
{
  // does not override the above method
  void somethingChanged(float data, int data2);
};

In face this problem in a current C++ project and the only thing you can
do is making the interface method pure virtual, so that it MUST be
implemented. If the interface changes after someone derived from it, the
compiler will spit out errors because not all pure virtuals are
implemented. The downside of this is that you must implement ALL methods
specified in the interface.

Another possibility would be to make one interface per callback, but
that seems cumbersome to me.

And the most convenient way, but also the way with the highest
bug-finding time, is to make all methods in the interface non-abstract.
This is nice for the user of the interface as they only need to
implement the callback they are interested in, but it gives no compile
time errors when subclasses have typos in the parameter lists, leaving
you with a silently failing program because of uncalled callbacks.

What I am looking for in D is some way to make sure that the method is
correctly overridden, but only IF it is overridden. So that not every
user of the interface has to override all methods, but the overriden
methods should be somehow made safe.

An idea would be to not allow deriving classes to declare a method with
a name already present in a parent class. So when the parent class
changes the parameter list of a virtual, subclasses won't compile
because they declare a method with the same name but a parameter list
that is not there in the parent.

Do I make sense? Any other ideas to solve this? Any way D does solve
this already maybe?

Regards,
Jörg Rüppel
Jan 12 2006
parent reply Sean Kelly <sean f4.ca> writes:
Jörg Rüppel wrote:
 Is there a way in D to make sure that interface methods are correctly
 overridden in subclasses?
 
 
 class ListenerIFace
 {
   void somethingChanged(int data, int data2);
 };
 
 
 class ChangeListener : public ListenerIFace
 {
   // does not override the above method
   void somethingChanged(float data, int data2);
 };
 
 In face this problem in a current C++ project and the only thing you can
 do is making the interface method pure virtual, so that it MUST be
 implemented. If the interface changes after someone derived from it, the
 compiler will spit out errors because not all pure virtuals are
 implemented. The downside of this is that you must implement ALL methods
 specified in the interface.
 
 Another possibility would be to make one interface per callback, but
 that seems cumbersome to me.
 
 And the most convenient way, but also the way with the highest
 bug-finding time, is to make all methods in the interface non-abstract.
 This is nice for the user of the interface as they only need to
 implement the callback they are interested in, but it gives no compile
 time errors when subclasses have typos in the parameter lists, leaving
 you with a silently failing program because of uncalled callbacks.

 What I am looking for in D is some way to make sure that the method is
 correctly overridden, but only IF it is overridden. So that not every
 user of the interface has to override all methods, but the overriden
 methods should be somehow made safe.
Use the "override" attribute in derived classes to make the compiler ensure that the same signature exists in a parent class: http://digitalmars.com/d/attribute.html#override Sean
Jan 12 2006
parent reply =?ISO-8859-1?Q?J=F6rg_R=FCppel?= <joerg sharky-x.de> writes:
Sean Kelly wrote:
 
 
 Use the "override" attribute in derived classes to make the compiler
 ensure that the same signature exists in a parent class:
 
 http://digitalmars.com/d/attribute.html#override
I fail to understand how someone can not fall in love with D.
Jan 12 2006
parent "Matthew" <matthew stlsoft.com> writes:
"Jörg Rüppel" <joerg sharky-x.de> wrote in message 
news:dq6of0$1g5m$1 digitaldaemon.com...
 Sean Kelly wrote:
 Use the "override" attribute in derived classes to make the compiler
 ensure that the same signature exists in a parent class:

 http://digitalmars.com/d/attribute.html#override
I fail to understand how someone can not fall in love with D.
LOL! :-)
Jan 12 2006