On Fri, 27 Apr 2012 09:15:31 -0400, so <[email protected]> wrote:

On Friday, 27 April 2012 at 12:35:53 UTC, Steven Schveighoffer wrote:

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!

With your proposal you can still do "f.open("world");" and get the same result if the author provided alias.

The point is, don't provide the alias. Why would anyone do that in this case?

You are trying to solve another problem, that the author should better state if this is intended. The problem i see is user assumming author is a smart guy. But at the end he finds out the author is as dumb as himself {he should have RTFM :)}

There is no protection D could ever provide against dumb authors ;) He could have named it "close", or "r72" and the compiler is powerless to prevent this! It's useless to try and prevent such things, D compiler is not a psychologist.

The problem I see here is that *smart* authors are inhibited from writing smart code.

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

-Steve

Easy! Don't call on an instance! openFile() out of the struct.
I always add "make_" before any static function, otherwise static methods should be precise as http://forum.dlang.org/post/[email protected]

I like having open inside the struct, just a matter of preference. I think File.open implies better than it returns a File more than openFile.

-Steve

Reply via email to