Arrays

Walter Bright newshound1 at digitalmars.com
Thu Mar 27 14:58:08 PDT 2008


Benji Smith wrote:
> ARRAYS
> 
> 1) There's a gap in functionality between static arrays (whose size must 
> be known at compile time) and growable dynamic arrays.
> 
> Most often, what I really want is a non-growable array whose size isn't 
> known until runtime, but D doesn't have that concept (Java and .NET have 
> it).

Java has the opposite issue - arrays cannot be grown at all. You have to 
allocate/copy the entire array every time. Andrei did propose a 
non-growable array last summer, but there was little interest in it.


> And the disparity between the static & dynamic array types (which 
> are fundamentally different in the type system) means I can't write code 
> like this:
> 
>   int[] doSomething(int[] array) { ... }
> 
>   int[4] a;
>   int[] b;
> 
>   // Inplicit conversion. Does this copy data?
>   b = doSomething(a);
> 
>   // Error: functions can't return fixed-length arrays!
>   a = doSomething(b);
> 
> That's really too bad. The type system really should treat an int[] and 
> an int[4] as the same type.

Actually, what we're going towards is having static arrays be value 
types, like structs are. This might be what you're asking for, although 
it would increase the semantic difference between T[] and T[4].


> If the compiler wants to optimize certain 
> static arrays by putting them in the data section of the compiled OBJ, 
> when possible, that's fine by me. But having that behavior affect the 
> type system is a pain in the neck.
> 
> I also would have preferred to have growable arrays and associative 
> arrays in the standard library than in the language. A more unified 
> array syntax could get rid of some nasty warts like this:
> 
>   // Doesn't compile
>   char[][] words = [ "hello", "world" ];
> 
>   // Compiles, but the only way to know about this trick is by asking
>   // someone for help in the NG.
>   char[][] words = [ "hello"[], "world" ];

That is just a fixable wart, not a fundamental issue.

>   // Would be ideal. Growable arrays would be best implemented as a
>   // templatized collection class.
>   List!(String) words = [ "hello, "world" ];
> 
> 2) An empty array is equal to a null pointer. Yikes!

Not exactly, although a null array is also an empty array, the reverse 
is not true.

> 3) Array syntax should support both multi-dimensional arrays and jagged 
> arrays:
> 
>   int[,] multidim = new int[4,5];
>   int[][] jagged = new int[4][5];

I agree that would be nice, but C++ doesn't do it either.



More information about the Digitalmars-d mailing list