Bizarre find() error [D2]

Jonathan M Davis jmdavisProg at gmail.com
Tue Feb 23 12:56:38 PST 2010


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


More information about the Digitalmars-d-learn mailing list