Static method conflicts with non-static method?

Steven Schveighoffer schveiguy at yahoo.com
Fri Apr 27 05:35:53 PDT 2012


On Fri, 27 Apr 2012 08:11:48 -0400, so <so at so.so> wrote:

> On Friday, 27 April 2012 at 11:51:40 UTC, Steven Schveighoffer wrote:
>
>> The idea I came up with in my proposal  
>> (http://d.puremagic.com/issues/show_bug.cgi?id=6579) was to allow  
>> aliasing the static method into the instance namespace:
>>
>> struct S1
>> {
>>    static void foo() {}
>>    alias S1.foo this.foo;
>> }
>>
>> struct S2
>> {
>>    static void foo() {}
>> }
>>
>> void main()
>> {
>>    S1 i1;
>>    S2 i2;
>>    S1.foo(); // ok
>>    i1.foo(); // ok
>>    S2.foo(); // ok
>>    i2.foo(); // error
>> }
>>
>> -Steve
>
> But call site remains unchanged, which was the main reason of confusion.  
> If we expect user to read the function declaration anyway, an extra  
> alias won't do much good or probably complicate it even further.

Huh?  The main reason of confusion is that the static method is named in  
such a way that it looks like an instance method.  So we prevent that,  
unless the author of the class (who is deciding the name of the function)  
deems it should be called on instances

example:

struct File
{
    static File open(string name) {...} // factory method
    this(string name);
}

File f = File("hello");

f.open("world"); // oops!  Just opened file world and threw it away
f = File.open("world");// better!

I challenge you to name File.open some way where it *wouldn't* be  
confusing when called on an instance :)

-Steve


More information about the Digitalmars-d mailing list