www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Bizarre find() error [D2]

reply Jonathan M Davis <jmdavisProg gmail.com> writes:
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/
td/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/
td/typecons.d(413):        instantiated from here: Tuple!(string,float)
/home/jmdavis/Downloaded_Files/dmd/dmd2/linux/bin/../../src/phobos/
td/typecons.d(423):        instantiated from here: slice!(1,3)
/home/jmdavis/Downloaded_Files/dmd/dmd2/linux/bin/../../src/phobos/st
/algorithm.d(1090):        3 recursive instantiations from here:
Tuple!(string[],uint)
/home/jmdavis/Downloaded_Files/dmd/dmd2/linux/bin/../../src/phobos/st
/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.

- Jonathan M Davis
Feb 22 2010
parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Tue, 23 Feb 2010 00:07:45 -0500, Jonathan M Davis  
<jmdavisProg 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/
td/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/
td/typecons.d(413):         
 instantiated from here: Tuple!(string,float)
 /home/jmdavis/Downloaded_Files/dmd/dmd2/linux/bin/../../src/phobos/
td/typecons.d(423):         
 instantiated from here: slice!(1,3)
 /home/jmdavis/Downloaded_Files/dmd/dmd2/linux/bin/../../src/phobos/st
/algorithm.d(1090):         
 3 recursive instantiations from here: Tuple!(string[],uint)
 /home/jmdavis/Downloaded_Files/dmd/dmd2/linux/bin/../../src/phobos/st
/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
Feb 23 2010
parent reply Jonathan M Davis <jmdavisProg gmail.com> writes:
Steven Schveighoffer wrote:
 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

Hmmm. It seems to have to do with calling the function with find in it from another file. And whether I do a find before it changes things. So, I have this in one file: immutable string[] FLAGS = ... void main(string[] args) { string[] flags; string[] files; parseArgs(args, flags, files); if(!checkFlags(FLAGS, flags)) return; ... } and this in another which is imported by the first: void parseArgs(string[] fullArgs, out string[] flags, out string[] args) { ... } bool checkFlags(string[] possibleFlags, string[] flags) { bool valid = true; foreach(flag; flags) { if(find(possibleFlags, flag).empty) { valid = false; writefln("Invalid flag: %s", flag); } } return valid; } This fails with the error that I posted before. However, if I add this line auto f = find(args, "") anywhere in main(), then it compiles. If I move the function in question into the main module, then it compiles. But if the function is in a separate module imported by the main one and find is not called in the main module, then it doesn't compile. I wouldn't have thought that the contents of the module with main in it would have any impact on the modules that its importing. And it doesn't seem to matter whether checkFlags() or anything else in the imported file actually gets used. And removing all of the other functions from the imported file doesn't change anything. It looks like merely using find in the imported file without having used it in the main one results in a compilation error. This is starting to look like a bug in DMD rather than my code. That is, unless I'm just totally misunderstanding something about how imports work. As it is, it just seems downright weird. - Jonathan M Davis
Feb 23 2010
parent reply bearophile <bearophileHUGS lycos.com> writes:
Jonathan M Davis:
 This is starting to look like a bug in DMD rather than my code. That is, 
 unless I'm just totally misunderstanding something about how imports work. 
 As it is, it just seems downright weird.

D2 module system has several bugs, but I can't know if the bug is in your code, in DMD, or it comes from misunderstanding of D. Can you please reduce the problem to some minimal, and show here the code of little modules that cause the bug? If it's a DMD bug it can then be filed in bugzilla. Bye, bearophile
Feb 23 2010
parent reply Jonathan M Davis <jmdavisProg gmail.com> writes:
bearophile wrote:

 Jonathan M Davis:
 This is starting to look like a bug in DMD rather than my code. That is,
 unless I'm just totally misunderstanding something about how imports
 work. As it is, it just seems downright weird.

D2 module system has several bugs, but I can't know if the bug is in your code, in DMD, or it comes from misunderstanding of D. Can you please reduce the problem to some minimal, and show here the code of little modules that cause the bug? If it's a DMD bug it can then be filed in bugzilla. Bye, bearophile

Okay. The first module: module main; import std.algorithm; import std.stdio; import other; void main(string[] args) { //auto f = find(args, ""); writefln("%s", contains(args, "hello")); } The second module: module other; import std.algorithm; import std.array; bool contains(string[] list, string str) { return find(list, str).empty; } If the commented out line in main() is uncommented, then the code compiles. Otherwise, you get this lovely error: /home/jmdavis/Downloaded_Files/dmd/dmd2/linux/bin/../../src/phobos/ td/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/ td/typecons.d(413): instantiated from here: Tuple!(string,float) /home/jmdavis/Downloaded_Files/dmd/dmd2/linux/bin/../../src/phobos/ td/typecons.d(423): instantiated from here: slice!(1,3) /home/jmdavis/Downloaded_Files/dmd/dmd2/linux/bin/../../src/phobos/st /algorithm.d(1090): 3 recursive instantiations from here: Tuple!(string[],uint) /home/jmdavis/Downloaded_Files/dmd/dmd2/linux/bin/../../src/phobos/st /algorithm.d(1205): instantiated from here: FindResult!(string[],string) other.d(8): instantiated from here: find!("a == b",string[],string) If you move the function in other.d into main.d (and add the import for std.array), then it works. It doesn't matter whether the commented out line is before or after the function call, or even whether it's in the same function. It doesn't matter whether the find is for the same types or not - trying to find an int in an int[] in main.d fixes the problem as well. If I add yet another module, like so: module main; import std.algorithm; import std.stdio; import other1; void main(string[] args) { //auto f = find(args, ""); writefln("%s", contains(args, "hello")); } The second module: module other1; bool contains(string[] list, string str) { return contains2(list, str); } The third module: module other2; import std.algorithm; import std.array; bool contains2(string[] list, string str) { return find(list, str).empty; } then the main module still has to have the find call, or the code doesn't compile. I can even make main() empty so that nothing is called, and it still won't compile - even if I add a find call in other1 (it doesn't matter whether main.d imports other1 either). After messing around with my build flags though, I _can_ say that it seems to have something to do with the -unittest flag. If that flag isn't used, then there's no problem. If it is used, then I get the compilation error. From what I see, it looks like this is a bug which needs to be reported as opposed to anything wrong with my code. - Jonathan M Davis
Feb 23 2010
parent Jonathan M Davis <jmdavisProg gmail.com> writes:
It looks to me like it might be the same issue as bug# 3682: 
http://d.puremagic.com/issues/show_bug.cgi?id=3682 - although I ran into it 
in a different way.

- Jonathan M Davis
Feb 23 2010