Non-Virtual Interfaces

Aleksandar Ružičić ruzicic.aleksandar at gmail.com
Fri Mar 4 16:05:54 PST 2011


>
> In D, the public function would have to be final to make it non-virtual/non-
> overridable, and the function it calls would have to be protected, since you
> can't override private functions (
> http://d.puremagic.com/issues/show_bug.cgi?id=4542 ). In this case, you're
> trying to override final functions, which doens't work at all.
>

Well I don't try to override final function, I know it cannot be done :)
I've just tried to override bar() in Foo when compiler told me that I
don't have that function implemented and I received message that it
cannot be overriden, as I've expected..

> However, if you're not trying to do anything other than call the implemented
> function (you're certainly not here), then there's no point to NVI. Just use a
> normal, public interface function or make the base class of your class abstract
> and put the function's declaration there.
>
> - Jonathan M Davis
>

That was just an example, what was my goal was is to have setup like this:

interface IEvent {
   EventType type();
}

interface IEventListener {
   void handle(IEvent event);
}

class MyEvent : IEvent {

   this(bool flag) {
     this.flag = flag;
   }

   EventType type() {
     return EventType.MyEvent;
   }

   bool isFlag() {
     return flag;
   }

   private:
      bool flag;
}

interface IMyEventListener : IEventListener {
    void onFlag(MyEvent event);
    void onNotFlag(MyEvent event);

    final void handle(IEvent event) {

       MyEvent e = cast(MyEvent) event;

       if (e !is null) {
         if (e.isFlag()) {
            onFlag(e);
         } else {
            onNotFlag(e);
         }
       }
    }
}

which would allow me to have a class that can listen for different
events at the same time, but it seems that to be able to do that I'd
have to move handling routine into the event class..


More information about the Digitalmars-d-learn mailing list