Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Stdout and Arrays

Moderators: kris

Posted: 02/17/08 14:40:24 Modified: 02/17/08 14:42:50

Hi,

I was writing a program and found sth that really puzzled me:

Let's consider a simple code like:

    uint[2][2][2] F;

    foreach(i, ref f; F)
        foreach(j, ref a; f)
            foreach (k, ref b; a) b = 100*i + 10*j + k;

It's obvious what it does - puts the index of each element into the element itself.

Now, when I try to print my array using:

    foreach(f;F) {
        Stdout.newline;
        foreach(a;f) Stdout(a);
    }

then I get:

[ 0, 1 ][ 10, 11 ]
[ 100, 101 ][ 110, 111 ]

which is exactly what I expected.

BUT...

when I try to print my array using:

    foreach(f;F) Stdout(f).newline;

then I get:

[ [ 0, 10 ], [ 10, 0 ] ]
[ [ 100, 110 ], [ 110, 0 ] ]

which is weird, because I thought that I'd get exactly the same thing as when using the double foreach loop. Moreover, some of the elements overlap, because for example there is only one 0 in my array, and there are three in the printout above.

I don't know what's happening. Is it a bug or am I doing sth wrong?


Tomek

Author Message

Posted: 02/26/08 23:40:30

Hi Tomek, (cześć? :))

This seems to be bug in Tango's, more exactly in tango.text.convert.Layout in processElement method, at the beginning there's code dealing with static arrays.

for (int i = 0; i < tiStat.len; i++)
{
    if (p !is _arg )
        length += sink (", ");
    processElement (tiStat.value, p);
    p += tiStat.tsize;
}

This requires just a small fix (since tiStat is TYPED pointer, incrementation should be done by size/type_size):

for (int i = 0; i < tiStat.len; i++)
{
    if (p !is _arg )
        length += sink (", ");
    processElement (tiStat.value, p);
    p += tiStat.tsize/tiStat.len;
}

If noone will respond, I can post ticket with this.

Posted: 02/27/08 00:31:44 -- Modified: 02/27/08 06:31:11 by
kris

Thank you, Gim. Ticket created and closed. Let us know if this does not fix the issue?