Naming things in Phobos - std.algorithm and writefln

Michel Fortin michel.fortin at michelf.com
Wed Aug 5 16:00:37 PDT 2009


On 2009-08-05 18:12:16 -0400, Bill Baxter <wbaxter at gmail.com> said:

> On Wed, Aug 5, 2009 at 2:40 PM, Andrei
> Alexandrescu<SeeWebsiteForEmail at erdani.org> wrote:
>> Sergey Gromov wrote:
>>> 
>>> Wed, 05 Aug 2009 17:29:11 +1000, Daniel Keep wrote:
>>> 
>>>> Michel Fortin wrote:
>>>>> 
>>>>> In std.algorithm, wouldn't it be clearer if "splitter" was called
>>>>> "splitLazily" or "splitLazy"? "splitter" is a noun, but as a function
>>>>> shouldn't it be a verb. "makeSplitter" or "toSplitter" perhaps?
>>>> 
>>>> This is a specious argument.
>>>> 
>>>> splitter's only purpose is to return an instance of a Splitter struct.
>>>> You can't call it "splitLazily" or "splitLazy" because that implies tha
> t
>>>> the function is doing work, when it really isn't.
>>> 
>>> That's if you know how it works.
>>> 
>>> But if you just use these functions, it's not even remotely obvious what
>>> the difference is, and the difference in naming is so subtle that many
>>> people will be doomed to forever confuse these functions, myself
>>> included.  I confuse getopt and getopts shell functions in the same wa
> y.
>>> I simply can't remember which is which.
>> 
>> Very true. If it weren't for backwards compatibility, I'd simply have
>> split() do the lazy thing. Then array(split()) would do the eager thing.
>> 
>> Andrei
>> 
> 
> Maybe introduce a convention like python and bearophile?  "foo" for
> eager things and "xfoo" for lazy things is what they use.  At least
> when you first see xfoo, you don't automatically assume you know what
> that "x" means, and go look it up if you don't know.

One question to ask is which one should be the default. If lazy should 
be the default then we want the lazy on to be called "split" and the 
non-lazy one to be called "eagerSplit" or whatever other convention for 
non-lazy. "str.eagerSplit()" would just be a shortcut for 
"str.split().toArray()".

Also, with implicit casts we wouldn't even need to bother about having 
a different names for lazy and non-lazy results, we could just do:

	string[] parts = str.split();

and it would implicitly convert the lazy range to an array. Can this be 
done with alias this? Would need to test.

	struct Range(T)
	{
		T[] toArray();
		alias toArray this;

		... other range things here...
	}

-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/




More information about the Digitalmars-d mailing list