www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Foreach type infering only works up to 2 levels?

reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
int[][][][] multiArray;

void main()
{
    foreach (one, two; multiArray)  // ok
    {
    }
    
    foreach (one, two, three; multiArray)  // fail
    {
    }    
}

test.d(13): Error: cannot infer type for two
test.d(13): Error: cannot infer type for three

Same thing happens with hashes as well. Is this a limitation in the language or
a bug?
Feb 02 2011
next sibling parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
Wow disregard this whole post. I've just realized how stupid that looks.

Sorry. :)
Feb 02 2011
parent reply Jesse Phillips <jessekphillips+D gmail.com> writes:
Andrej Mitrovic Wrote:

 Wow disregard this whole post. I've just realized how stupid that looks.
 
 Sorry. :)
See, you have material for a tutorial. I know you wouldn't run out.
Feb 02 2011
parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 2/2/11, Jesse Phillips <jessekphillips+D gmail.com> wrote:
 Andrej Mitrovic Wrote:

 Wow disregard this whole post. I've just realized how stupid that looks.

 Sorry. :)
See, you have material for a tutorial. I know you wouldn't run out.
Actually, yes. TDPL doesn't discuss much about multidimensional data, other than the simple 2-dimensional arrays (IIRC).
Feb 02 2011
prev sibling parent reply Mafi <mafi example.org> writes:
Am 02.02.2011 20:16, schrieb Andrej Mitrovic:
 int[][][][] multiArray;

 void main()
 {
      foreach (one, two; multiArray)  // ok
      {
      }

      foreach (one, two, three; multiArray)  // fail
      {
      }
 }

 test.d(13): Error: cannot infer type for two
 test.d(13): Error: cannot infer type for three

 Same thing happens with hashes as well. Is this a limitation in the language
or a bug?
I use the following idiom. foreach(x, ref column; array) foreach(y, ref element; column) //maybe more... { //do something } It works because of "constructs take one staement & one block is one statement". It is really great because it leads to such flexible soultions. You might say it's bad practise bad then you have to say the same about 'else if'. Mafi
Feb 02 2011
parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 2/2/11, Mafi <mafi example.org> wrote:
 foreach(x, ref column; array)
 foreach(y, ref element; column)
 //maybe more...
 {
      //do something
 }
Cool, that fixes the indentation issue. Nice.
Feb 02 2011