www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - dynamic class variable/method

Hi:

Even if it can be done by maybe mixins, I still think there could be a class
variable or method that can be overridden. I suppose there is a keyword called
dynamic, which is just the same as static, with the exception that it can be
overridden by subclasses.

class A
{
static const int x = 0;
dynamic const int y = 0;
}

class B : A
{
static const int x = 10;
override dynamic const int y = 10;
}

void main()
{
A a = new A();
printf("%d\n", a.x);  // 0, in this case the constant cannot be overridden
printf("%d\n", a.y);  //10, in this case the constant is overridden
}

Although it could be done by mixins with a virtual function, the syntax shown
above is more readable and clear. Lots of the programming languages take class
variable and methods as static and non-overridden, but sometimes you might need
one that should be able to override, and I think it could be implemented easily
in the language feature.
Jun 07 2006