January 30, 2014 Re: Why can't a method be virtual AND static at the same time? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Martin Cejp | On Wednesday, 29 January 2014 at 14:24:12 UTC, Martin Cejp wrote: > (though there's no way around having to use 2 different names for the class and the const instance, is there?) Actually, there is: http://dlang.org/class.html#anonymous But I'm not sure if it works at module level. |
January 30, 2014 Re: Why can't a method be virtual AND static at the same time? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Martin Cejp | On Wed, 29 Jan 2014 08:30:53 -0500, Martin Cejp <minexew@gmail.com> wrote:
> This is a feature I've always missed in C++. Consider the code
> below:
>
> import std.stdio;
>
> interface Logger {
> void print(string msg);
> }
>
> class ConsoleLogger : Logger {
> static override void print(string msg) {
> writeln(msg);
> }
> }
>
> void main() {
> Logger logger = new ConsoleLogger;
>
> ConsoleLogger.print("Hello, World!");
> }
>
> Such definition of ConsoleLogger fails to compile. I don't see
> any drawbacks to allowing this though, except the compiler would
> probably have to generate 2 methods internally.
> The way it is now, you have to either define each method twice or
> always create an instance.
> Or am I missing an obvious solution?
Interface Logger {
final void print(string msg) { printImpl(msg); }
void printImpl(string msg);
}
class ConsoleLogger : Logger {
static void print(string msg) {
writeln(msg);
}
override void printImpl(string msg) {
print(msg);
}
}
The issue you have is with the naming, you can't overload a virtual function with a static one. A static function call is a different call than a virtual one. You can't mix the two.
-Steve
|
January 30, 2014 Re: Why can't a method be virtual AND static at the same time? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Martin Cejp | 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 |
January 30, 2014 Re: Why can't a method be virtual AND static at the same time? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Thursday, 30 January 2014 at 13:24:09 UTC, Steven Schveighoffer wrote: [snip] > > The issue you have is with the naming, you can't overload a virtual function with a static one. A static function call is a different call than a virtual one. You can't mix the two. > > -Steve Actually you can do this, see my other post. Overloading a static method with a virtual method or vice-versa works fine. You cannot do is override a static method because it isn't virtual and is nonsensical IMO. The OP said they expected overloading to occur in response to another post: [snip] > What do you mean? The methods have different signatures (static x virtual) [snip] so I don't know why they were thinking to override, probably just a simple mistake. Cheers, ed |
Copyright © 1999-2021 by the D Language Foundation