www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - int[][] better type match than int[] for int[]?!

reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
This completely surprised me:

bool doMatch(int[] lhsArr, int[][] arrArr)
{
    foreach (int[] rhsArr; arrArr)
    {
        writeln("if (!doMatch(lhsArr, arrArr))");

        if (!.doMatch(lhsArr, arrArr))
            return false;
    }

    return true;
}

bool doMatch(int[] lhsArr, int[] rhsArr)
{
    return true;
}

void main()
{
    int[] x   = [1, 2];
    int[][] y = [[1, 2], [1, 2]];

    bool b = doMatch(x, y);
}

This will enter an infinite loop because the first doMatch overload
gets recursively called. I don't understand why the second overload
isn't picked up as a match in the call "doMatch(lhsArr, arrArr)".
Sep 17 2012
parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Mon, 17 Sep 2012 09:21:41 -0400, Andrej Mitrovic  
<andrej.mitrovich gmail.com> wrote:

 This completely surprised me:

 bool doMatch(int[] lhsArr, int[][] arrArr)
 {
     foreach (int[] rhsArr; arrArr)
     {
         writeln("if (!doMatch(lhsArr, arrArr))");

         if (!.doMatch(lhsArr, arrArr))
Don't you mean: if(!.doMatch(lhsArr, rhsArr)) ??
             return false;
     }

     return true;
 }

 bool doMatch(int[] lhsArr, int[] rhsArr)
 {
     return true;
 }

 void main()
 {
     int[] x   = [1, 2];
     int[][] y = [[1, 2], [1, 2]];

     bool b = doMatch(x, y);
 }

 This will enter an infinite loop because the first doMatch overload
 gets recursively called. I don't understand why the second overload
 isn't picked up as a match in the call "doMatch(lhsArr, arrArr)".
Because arrArr is an int[][] :) -Steve
Sep 17 2012
parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 9/17/12, Steven Schveighoffer <schveiguy yahoo.com> wrote:
 Don't you mean:

 if(!.doMatch(lhsArr, rhsArr))
yessssssss.. oh man I need reading glasses! Or an IDE to warn me of my own mistakes! (preferably one that doesn't crash). Another thread to nuke. :P
Sep 17 2012