Overloading static methods

mta`chrono chrono at mta-international.net
Sat Sep 24 13:26:34 PDT 2011


Quote: Steven Schveighoffer
> An example I gave in the bug report just now is File.  Imagine you have
> a File struct, and want to have an open method:
> 
> struct File
> {
>    static File open(string fname);
> }
> 
> However, now this is valid code:
> 
> File f; // <-------- here's the problem!
> f.open(fname);  // does not do what you think it does...
> 

Yes, but why is he able to get an instance of File? The Designer of
"File" should have rather used a class and disallow to create any
instance of it. (@disable this)

If instantiation should be only possible through a static function, then
implement a private constructor or disable it.

Quote: Andrei Alexandrescu
> I'll note that calls of static methods for instances has been a boon to
> generic programming in C++. People could call a method and it was up to
> the implementation whether it was static or not.

This is a D feature. It's up to the designer to implement it static or
non-static way. You don't have to care about, just call it.


More information about the Digitalmars-d mailing list