Overloading static methods

Steven Schveighoffer schveiguy at yahoo.com
Tue Aug 30 09:19:20 PDT 2011


On Tue, 30 Aug 2011 11:58:20 -0400, Steven Schveighoffer  
<schveiguy at yahoo.com> wrote:

> On Tue, 30 Aug 2011 11:38:54 -0400, Andrei Alexandrescu  
> <SeeWebsiteForEmail at erdani.org> wrote:
>
>> We don't have as big a problem in D due to introspection. I fear,  
>> however, that we'll need to add static if (...) obj.method(); else  
>> typeof(obj).method();
>>
>> I don't see an improvement.
>
> In terms of generic programming, you have a good point.  I don't see a  
> good way around this without altering syntax.
>
> Perhaps we could do something like this (a la recently added @disable  
> this):
>
> struct File
> {
>     static File open(string fname);
>
>     @disable open; // disable calling from an instance
> }
>
> Yuck...  nevermind :)

Hm... this might be workable.  Disallow calling static method from  
instance unless requested by the author:

struct S1
{
    static void foo();
}

struct S2
{
    static void foo();
    alias foo this.foo;
}

-Steve


More information about the Digitalmars-d mailing list