startsWith using an array of needles

Jonathan M Davis jmdavisProg at gmx.com
Tue Aug 20 12:16:39 PDT 2013


On Tuesday, August 20, 2013 19:32:57 bearophile wrote:
> Jonathan M Davis:
> > startsWith dosen't take an array of needles. It takes a
> > variadic list of them.
> 
> All the argument after the first of startsWith could be required
> to be of the same type, for this D offers:
> 
> void foo(T)(T[] items...) {}
> void main() {
> foo(1);
> foo(1, 2);
> foo(1, 2, 3);
> foo([1]);
> foo([1, 2]);
> foo([1, 2, 3]);
> }
> 
> 
> Isn't it better to modify startsWith a little like this?

Things get messy when you try and do stuff like that thanks to being able to 
have arrays of arrays and whatnot. We already have issues with several 
functions where you're forced to give them strings instead of characters. 
Type-safe variadics are particularly bad thanks to the fact that they acccept 
both indivdual elements and an array.

Also, allowing what you suggest would increase the risk of bugs when you meant 
to do something like search for a string in an array of strings but 
accidentally give it a string to search in. Right now, stuff like that gets 
caught.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list