Hi,everyone,
Here has a error after run: " main.exe 11 " or " main.exe 10 ":

module main;
import std.stdio,std.conv;

template find(T)
{
    size_t find(T[] Array,T Element)
        {
                size_t i;
        foreach(T ArrayElement; Array)
                {
            if(Element==ArrayElement)
                        {
                                return i;
                        }       
                        i++;
        }
    return -1;
    }
}

void main(string[] args)
{
    int[] stuff=[0,1,2,3,4,5,6,7,8,9,10];
        try{
                
     if(auto m =(stuff.find(to!int(args[1]))>=0))
    {
        writeln(stuff," contains ",args[1]);
    }
        else
        {
                writeln(stuff," not contains ",args[1]);
        }
}

the exe result: is  contains 11 ← Error

 but this is ok:

void main(string[] args)
{
    int[] stuff=[0,1,2,3,4,5,6,7,8,9,10];
        try{
                int i =stuff.find(to!int(args[1]));
                auto x = stuff.find(to!int(args[1]))>=0;
                writeln("typeof(x) is :",x);
    if (i>=0)
                
        //if(auto m =(stuff.find(to!int(args[1]))>=0))
    {
        writeln(stuff," contains ",args[1]);
    }
        else
        {
                writeln(stuff," not contains ",args[1]);
        }
}
Why?

Thank you.

Frank

Reply via email to