Implement @disable methods as always-throw methods?

Andrej Mitrovic andrej.mitrovich at gmail.com
Sun Oct 2 10:41:27 PDT 2011


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?


More information about the Digitalmars-d mailing list