Bizarre find() error [D2]

Steven Schveighoffer schveiguy at yahoo.com
Tue Feb 23 04:36:04 PST 2010


On Tue, 23 Feb 2010 00:07:45 -0500, Jonathan M Davis  
<jmdavisProg at gmail.com> wrote:

> Okay, I'm trying to find out if a particular string is in an array of  
> strings. The best function that I can find for that appears to be  
> find(), since there's no contains() or anything
> like that and in only works for associative arrays. So, my code is  
> something similar to this:
>
>
> string[] list;
> string str;
> auto f = find(list, str);
> bool strInList = !f.empty;
>
> However, I get a really weird error on the line with find():
>
> /home/jmdavis/Downloaded_Files/dmd/dmd2/linux/bin/../../src/phobos/std/typecons.d(424):  
> Error: static assert  (is(Tuple!(string,float) == Tuple!(string,float)))  
> is false
> /home/jmdavis/Downloaded_Files/dmd/dmd2/linux/bin/../../src/phobos/std/typecons.d(413):         
> instantiated from here: Tuple!(string,float)
> /home/jmdavis/Downloaded_Files/dmd/dmd2/linux/bin/../../src/phobos/std/typecons.d(423):         
> instantiated from here: slice!(1,3)
> /home/jmdavis/Downloaded_Files/dmd/dmd2/linux/bin/../../src/phobos/std/algorithm.d(1090):         
> 3 recursive instantiations from here: Tuple!(string[],uint)
> /home/jmdavis/Downloaded_Files/dmd/dmd2/linux/bin/../../src/phobos/std/algorithm.d(1205):         
> instantiated from here: FindResult!(string[],string)
> utils.d(43):        instantiated from here: find!("a ==  
> b",string[],string)
>
>
> I assume that the first line comes from some sort of type checking of  
> the expression, but the type is identical on both sides of the ==, so I  
> don't see how that could be false. Any
> ideas as to what I'm doing wrong? Or is there a bug in the phobos code?
>
> Also, is there a better function than find for determining whether  
> something is in a range (I can't find one)? I don't care about returning  
> the range starting at the point that the
> element was found or getting an index for it or anything like that. I  
> just want a boolean value as to whether it's in the range or not.

This works for me (dmd 2.040):

import std.algorithm;
import std.array;

void main()
{
     string[] list;
     string str;
     auto f = find(list, str);
     bool strInList = !f.empty();
}

-Steve


More information about the Digitalmars-d-learn mailing list