Bug 5807 – Disallow mixed C/D style array declarations

Status
RESOLVED
Resolution
WONTFIX
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2011-04-03T10:33:53Z
Last change time
2020-03-21T03:56:39Z
Keywords
bootcamp
Assigned to
No Owner
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2011-04-03T10:33:53Z
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 by b2.temp — 2019-11-03T18:51:48Z
Just do this in D-Scanner as a new check, it's an easy one.