July 05, 2013
> Fixing it so that the sizes for static arrays were read left-to-right would
> definitely be a usability improvement, but even if we were to decide that the
> whole "read the type outward from the variable name" was unimportant enough to
> make the change desirable, it would silently break all kinds of code at this
> point, so we're stuck with it.
>
The way D declares static arrays makes sense and I was actually trying to access elements in the same fashion until I realized that it's reversed. I than found a remark in Andrei's book pointing out the difference.

Thanks for your reply Jonathan.

   --Oleksiy
July 06, 2013
> However, that is a confusing syntax because the right-hand side is not the same type as the elements, which is dchar[3]. Perhaps D supports it for C compatibility?

Yes, I noticed:
arr = '!';

Error: cannot implicitly convert expression ('!') of type char to dchar[3u][]

Look like there is a consequence - can't perform array-wise operations on multidimensional arrays? And that's why a need to touch the elements themselves, like in the example you provided:

import std.stdio;
>
> void main()
> {
>     dchar[3][5] arr = '.';
>
>     foreach (ref e; arr) {
>         e[2] = '!';
>     }
>
>     writeln(arr);
> }

>   a[] alone is a slice to all of the elements of 'a'. When you use that syntax in an operation, then that operation is applied "array-wise":
>
>   a[] = b[] + c[];
>
> That is the same as
>
>   a[i] = b[i] + c[i]
>
> for all valid values of 'i'.
I truly got what the problem is after this part. Thanks for a very detailed explanation.

Also huge thank you for your book - holy grail for beginners.
--------------------------------
> But this doesn't compile:
>
>     char[3][5] arr = [ '.', '.', '.' ];
>
> Error: mismatched array lengths, 15 and 3
>
> I see that as a bug but can't be sure.

Others seem to agree with you, will you be submitting this bug?

   --Oleksiy

July 06, 2013
On Friday, 5 July 2013 at 01:31:00 UTC, Ali Çehreli wrote:
> But this doesn't compile:
>
>     char[3][5] arr = [ '.', '.', '.' ];
>
> Error: mismatched array lengths, 15 and 3
>
> I see that as a bug but can't be sure.

I'd file a bug report, but since [x, y, z] is primarily a dynamic array literal, I'd file it like this:

struct S
{
    int[3] values;
}

void main()
{
    alias A = S;
    alias B = int[3];

    A[2] a = A.init; // OK
    B[2] b = B.init; // Error: mismatched array lengths, 6 and 3
}
July 07, 2013
On 07/05/2013 09:00 PM, Oleksiy wrote:

> Look like there is a consequence - can't perform array-wise operations
> on multidimensional arrays?

I don't want to accept that conclusion because there is no such thing as a multidimensional array in D. :) (To be fair, they don't exist in C and C++.)

What we call a multidimensional array is achieved by defining arrays where the elements are arrays themselves. But D does not know or care about that. As a result, array-wise operations apply only to the outermost layer of so-called multidimensional arrays.

Let's have the following two-dimensional arrays:

int[][] a;
int[][] b;
int[][] c;

We can still use the array-wise syntax:

c[] = a[] OP b[];

Let's think about what can appear as an operator instead of OP above... Since the elements of the outer layers of the array are themselves arrays (more correctly, slices), then OP must be an operator that can have two slices on its side. The concatenation operators comes to mind but I could not make it work:

c[] = a[] ~ b[];

That should have the same effect as concatenating the corresponding elements of a and b and assigning the result to c.

It does not work.

> And that's why a need to touch the elements
> themselves, like in the example you provided:
>
> import std.stdio;
>>
>> void main()
>> {
>>     dchar[3][5] arr = '.';
>>
>>     foreach (ref e; arr) {
>>         e[2] = '!';
>>     }
>>
>>     writeln(arr);
>> }

That example is doing what you needed to do: Assign a specific value to a column of the multidimensional array. Unfortunately, there is no special syntax in D to do that.

> Also huge thank you for your book - holy grail for beginners.

Thank you very much for the kind words; makes me very happy.

> --------------------------------
>> But this doesn't compile:
>>
>>     char[3][5] arr = [ '.', '.', '.' ];
>>
>> Error: mismatched array lengths, 15 and 3
>>
>> I see that as a bug but can't be sure.
>
> Others seem to agree with you, will you be submitting this bug?

As TommiT did, it is better to try a code that has a fixed-length array on the right-hand side:

void main()
{
    int[3] values = [ 1, 2, 3 ];

    int[3][2] a = values;
}

Error: mismatched array lengths, 6 and 3

Reported:

  http://d.puremagic.com/issues/show_bug.cgi?id=10562

Ali

1 2
Next ›   Last »