between and among: worth Phobosization?

Marco Leise Marco.Leise at gmx.de
Tue Dec 17 06:36:26 PST 2013


Am Mon, 16 Dec 2013 14:59:43 -0800
schrieb Andrei Alexandrescu <SeeWebsiteForEmail at erdani.org>:

> On 12/16/13 2:38 PM, Andrej Mitrovic wrote:
> > On 12/16/13, Andrei Alexandrescu <SeeWebsiteForEmail at erdani.org> wrote:
> >> There's quite a bit of evidence in support of among (not as much for
> >> between) in the source code of Facebook's cpplint, soon to be open
> >> sourced. Here are some relevant quotes:
> >
> >>From first glance it seems you're always using among when needing a
> > boolean result, so why does among have to return an index instead of a
> > boolean true/false?
> 
> More info is better than less info, especially if it comes for free. We 
> already use this idiom in a couple of places in Phobos.
> 
> Andrei

I've always preferred findCharInString() to return the "end"
of the string in case the char isn't found instead of e.g. -1,
for two reasons:

a) With an index to the end (equal to the length) you can do
   more useful stuff like:

   auto idx = findCharInString(' ', str);
   str = str[idx .. $];

   -1 requires an explicit if()-branch in any case.

   auto idx = findCharInString(' ', str);
   if (idx == -1) {
     str = ""
   } else {
     str = str[idx .. $];
   }

b) The index becomes size_t again, which is the correct type.

Now similarly I would prefer if among() could do anything else
but shift indexes by +1. Indexes in D are 0 based. Since we
are talking about a verbatim set of options here instead of a
linear string, I could live with -1 as "not in list" and a
zero based index, but then among would have to be renamed to
something that doesn't imply a result that converts to boolean.
"optionIndex" or something.

-- 
Marco



More information about the Digitalmars-d mailing list