www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - disable on override function

reply Jesse Phillips <jessekphillips+D gmail.com> writes:
 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();
}
Nov 05 2010
parent "Simen kjaeraas" <simen.kjaras gmail.com> writes:
Jesse Phillips <jessekphillips+D gmail.com> wrote:

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

I think this is the best solution. "Cannot disable overriding functions", I'd say. -- Simen
Nov 05 2010