Mixing C/D-style array declarations

bearophile bearophileHUGS at lycos.com
Sun Apr 3 10:43:59 PDT 2011


After a suggestion from simendsjo in D.learn, I have added an enhancement request:
http://d.puremagic.com/issues/show_bug.cgi?id=5807

If you see something wrong in this post I will modify or close the enhancement request.

The documentation about Postfix Array Declarations says:

> Rationale: The postfix form matches the way arrays are declared
> in C and C++, and supporting this form provides an easy migration
> path for programmers used to it.<

C style array declarations are handy (despite they add a little of extra complexity and confusion to the D language).

But currently (DMD 2.052) D also supports a syntax like:

// array of 5 dynamic arrays of ints.
int[][5] c;
int[] c[5];
int c[5][];

What's the purpose of such mixed syntax?
1) It's not present in C programs;
2) It's not used in normal native D programs;
3) And in my opinion it's not good to use even during the migration from C to D style, because it looks confusing (the order of ranges is inverted between C and D syntax).

So I suggest to statically disallow such mixed syntax.


Currently the mixed syntax is not just useless and potentially confusing for the programmer, it also causes problems with some vector operations:

void main() {
    int[] a1 = [1, 2, 3];
    int[] a2 = new int[3];
    a2[] = a1[];           // OK
    int[3] a3[] = a2[];    // line 5, Error
}

test.d(5): Error: cannot implicitly convert expression (a2[]) of type int[] to int[3u][]

In theory if mixed C/D array declarations become disallowed, then there is only one meaning for the operation at line 5. (This is not very nice, but I don't see better solutions for this syntax problem with array operations).

Bye,
bearophile


More information about the Digitalmars-d mailing list