@disable on override function

Jesse Phillips jessekphillips+D at gmail.com
Fri Nov 5 11:47:01 PDT 2010


@disable provides the ability to prevent a function from being called at compile time. Due to inheritance and the ability to assign references to base class objects, it is possible to execute code that has been disabled.

class A {
   void hello() {
   }
}
class B : A {
   @disable override void hello() {
   }
}

void main() {
   auto a = new A();
   A b = new B();

   b.hello();
}

I submitted it as a bug: http://d.puremagic.com/issues/show_bug.cgi?id=5171
But as this can not be checked statically I'm asking what might be the best option?

Should this behavior remain? It does prevent the function from being called with a reference to B.

Should it insert an assert(0) so that it fails during runtime?

Should the compiler not compile the class saying something to the effect of:
Can not disable method hello in base class A from B.

Note that I think the code below should still compile:

class A {
   @disable void hello() {
   }
}
class B : A {
   override void hello() {
   }
}

void main() {
   auto a = new A();
   B b = new B();

   b.hello();
}


More information about the Digitalmars-d mailing list