input validation

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Wed Mar 4 10:47:06 PST 2009


bearophile wrote:
> Andrei Alexandrescu:
>> The approach I took with the new phobos is:
>> 
>> int[] haystack; int[] needle; ... auto pos1 = find(haystack,
>> needle); // linear sort(haystack); auto pos2 =
>> find(assumeSorted(haystack), needle);
> 
> In my dlibs I do in a simpler and shorter way:
> 
> auto pos1 = haystack.index(needle); haystack.sort(); auto pos2 =
> haystack.bisect(needle);
> 
> Here there's no need to give the same name to two very different
> functions.

They do the exact same thing. Unifying them under the same name is good
abstraction.

> If you really like to use a single function mame, with named
> arguments you may also do (bisect is false by default):
> 
> auto pos1 = haystack.index(needle); haystack.sort(); 
 > auto pos2 = haystack.index(needle, bisect=true);

I prefer encoding structural information in types.


Andrei



More information about the Digitalmars-d mailing list