Implement @disable methods as always-throw methods?

Timon Gehr timon.gehr at gmx.ch
Sun Oct 2 10:59:01 PDT 2011


On 10/02/2011 07:41 PM, Andrej Mitrovic wrote:
> I'm curious about this case:
>
> import std.stdio;
>
> abstract class Widget
> {
>      void add(Widget w)    { writeln("add");    }
>      void remove(Widget w) { writeln("remove"); }
> }
>
> class Leaf : Widget
> {
>      @disable override void add(Widget Widget)    { writeln("disabled"); }
>      @disable override void remove(Widget Widget) { writeln("disabled"); }
> }
>
> void main()
> {
>      Widget leaf = new Leaf;
>      leaf.add(new Leaf);
> }
>
> The compiler can't catch this since 'leaf' has the static type Widget,
> so Leaf's virtual function will still get called. Of course you could
> cast leaf to Leaf, but that's beside the point that a disabled method
> ultimately gets called.
>
> I was thinking, would it be a good idea for the compiler to implement
> disabled methods as always-throw methods? If it can't catch them being
> called at compile time then at least you would get a runtime error if
> they were to throw.
>
> Thoughts?

Imho disabling overriden member functions is unsound and should be 
disallowed entirely.


More information about the Digitalmars-d mailing list