'in' syntax for searching arrays

Matti Niemenmaa see_signature at for.real.address
Sun Nov 4 06:13:39 PST 2007


Janice Caron wrote:
> V[] a;
> int n = a.find(x);
> 
> should return -1 if the value is not present in the array, or the
> index if it is. This is a much better idea than returning a pointer to
> the element, because (a) you don't have to worry about const and
> invariant, and (b) it's the same as std.string. In addition, you can
> then do a[n..$].find() to find the second occurence, and so on.

As an aside: Tango uses the idiom that find returns the length of the given
array if no match is found. This allows it to return a size_t (if you ever use
arrays longer than int.max...) whilst still having a clear error value.

-- 
E-mail address: matti.niemenmaa+news, domain is iki (DOT) fi



More information about the Digitalmars-d mailing list