D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 5807 - Disallow mixed C/D style array declarations
Summary: Disallow mixed C/D style array declarations
Status: RESOLVED WONTFIX
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 enhancement
Assignee: No Owner
URL:
Keywords: bootcamp
Depends on:
Blocks:
 
Reported: 2011-04-03 10:33 UTC by bearophile_hugs
Modified: 2020-03-21 03:56 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2011-04-03 10:33:53 UTC
An idea originally from simendsjo, then expanded.

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).
Comment 1 basile-z 2019-11-03 18:51:48 UTC
Just do this in D-Scanner as a new check, it's an easy one.