Thread overview
Foreach type infering only works up to 2 levels?
Feb 02, 2011
Andrej Mitrovic
Feb 02, 2011
Andrej Mitrovic
Feb 02, 2011
Jesse Phillips
Feb 02, 2011
Andrej Mitrovic
Feb 02, 2011
Mafi
Feb 02, 2011
Andrej Mitrovic
February 02, 2011
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?
February 02, 2011
Wow disregard this whole post. I've just realized how stupid that looks.

Sorry. :)
February 02, 2011
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.
February 02, 2011
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).
February 02, 2011
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
February 02, 2011
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.