Why can't a method be virtual AND static at the same time?

Vitali notavailable at mail.com
Thu Jan 30 13:51:16 PST 2014


Your motivation for this question is not clear. I can only guess.

If you hope to get more performance by providing only one
function, which is used as virtual and static at the same time,
then this is impossible, because virtual functions are bound to a
data instance by design.

If you look for a possibility to type less code by skipping the
instantiation of a class, then the use of an interface may be the
wrong decision.

If you want the virtual function to behave the same as the static
function, then you can wrap the static function with the virtual
one, like this:

class ConsoleLogger : Logger {
    void print(string msg) {
      staticPrint(msg);
    }
    static void staticPrint(string msg) {
      writeln(msg);
    }
}

If you look for a possibility to override a static function like
a virtual one, then you may be interested in so called "system
mixins" (not part of D). System mixins are researched in aspect
oriented programming (AOP). Read about it in this paper:
Ichisugi, Y., Roudier, Y. (1998) Mixin Composition Strategies for
the Modular
Implementation of Aspect Weaving, Kyoto: Aspect Oriented
Programming workshop at
ICSE'98


More information about the Digitalmars-d mailing list