www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Abstract static methods

Hi all. I'm new to D and this is my first time posting here so please forgive me
if this topic has been covered before. I looked this up in the D spec at the
DigitalMars website and as far as I can tell it is not a feature of D so I
thought I'd post my thoughts about it in the hopes that perhaps a future version
of D can include it. I'm also very curious to read other programmers' opinions
on the usefulness of this language feature and whether or not the same effect

ado...

I'm writing a UI library in C++ and want the various components/control TYPES to
register themselves as part of a component set. Note that I want their types
registered, not individual instances.

So, to make registration a class-level operation I use a static method:

class Component
{
public:
static bool RegisterType(ComponentSet* compSet);
static Component* Create();
};

The body should look something like this for each derived component:

// class Button: public Component
bool Button::RegisterType(ComponentSet* compSet)
{
compSet->Register("Button", &Create);
}

Button* Button::Create()
{
return new Button();
}


// class TextBox: public Component
bool TextBox::RegisterType(ComponentSet* compSet)
{
compSet->Register("TextBox", &Create);
}

TextBox* TextBox::Create()
{
return new TextBox();
}


As you can see I want each class derived from Component to provide their own
implementations of the static methods RegisterType and Create. The problem is
that in C++ there is no way to FORCE programmers who derive from Component to
re-write Component's static methods. That means that someone could derive a new
component and forget to provide some vital functionality.

What I would love to see in a language is a way to have a sort of "abstract
static" method. Using C++ syntax it would look like this:

class Component
{
public:
static bool RegisterType(ComponentSet* compSet) = 0;
static Component Create() = 0;
};

The compiler would then require any derived classes to implement their own
versions of the static methods.

-Mike-
Apr 27 2004