www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Mixing C/D-style array declarations

reply bearophile <bearophileHUGS lycos.com> writes:
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
Apr 03 2011
next sibling parent spir <denis.spir gmail.com> writes:
On 04/03/2011 07:43 PM, bearophile wrote:
 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).
+++ D cleaning. Denis -- _________________ vita es estrany spir.wikidot.com
Apr 03 2011
prev sibling next sibling parent reply "Vladimir Panteleev" <vladimir thecybershadow.net> writes:
On Sun, 03 Apr 2011 20:43:59 +0300, bearophile <bearophileHUGS lycos.com>  
wrote:

 C style array declarations are handy (despite they add a little of extra  
 complexity and confusion to the D language).
Hmm, I thought C-style array declarations are pending deprecation. I know the C-style function pointer syntax has already been deprecated. -- Best regards, Vladimir mailto:vladimir thecybershadow.net
Apr 03 2011
parent bearophile <bearophileHUGS lycos.com> writes:
Vladimir Panteleev:

 Hmm, I thought C-style array declarations are pending deprecation.
Is this true? If this is true then I will close my new enhancement request. Bye, bearophile
Apr 03 2011
prev sibling parent Trass3r <un known.com> writes:
 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).
Are you kidding? It pollutes the grammar and breaks consistency. The rationale is nonsense, it rather encourages people not to learn the D way to declare arrays. Drop the C legacy!
Apr 03 2011