Dynamic arrays in D 1.0

Bill Baxter dnewsgroup at billbaxter.com
Sun May 11 12:24:50 PDT 2008


Edward Diener wrote:
> In D 1.0 dynamic arrays are the equivalent of C++'s std::vector<T>, with 
> slicing replacing iterators in order to access some subrange of the 
> sequence. In C++ there is functionality in std::vector::erase for 
> erasing elements from the vector and std::vector::insert for inserting 
> new elements into the vector. Where is that functionality in D's dynamic 
> arrays ?

Yes, erase & insert and many other things are conspicuously missing from 
D1 Phobos (not sure about D2 Phobos).  I like the collection of such 
routines provided by the Cashew library in the cashew.utils.Array module.

> Furthermore in D 1.0 a char[], a dynamic array of characters, is the 
> equivalent of the C++ std::string. In the C++ std::string class there is 
> a rich set of functionality for manipulating the char elements in the 
> string, for finding particular elements in the string, and for comparing 
> the character elements in strings with other strings, but I see little 
> of this in dynamic array functionality. What am I missing ?

Those functions are in std.string in Phobos.  And scattered all over the 
place in Tango.

> I am guessing this must be provided in D libraries of functions ( Phobos 
> ? Tango ? ) templated on the dynamic array type. 

Right.  Except for generic arrays, there's nothing in D1 Phobos.

> But, if so, this seems 
> a poor way of providing dynamic array functionality as compared to 
> built-in dynamic array functionality in order to provide the sort of 
> functionality mentioned above, especially as D's dynamic arrays are part 
> of the D language as opposed to a separate library like C++'s 
> std::vector<T> and std::string.

Why?  What's wrong with providing such functionality as a library when a 
library does the trick?

Are you aware of the pseudo-member trick that works for arrays?  Example:

   T get_elem(T)(T[] array, size_t idx) {
      return array[idx];
   }
   ...
   float[] numbers = [1.f, 2,3];
   float first = numbers.get_elem(0);

That works today in D1 and D2.  Any function that takes an array as a 
first element can be treated as if it were a (non-virtual) member of the 
array type.

So given that, I think there's really no reason for the bulk of D's 
array functionality to not be in a library.

--bb



More information about the Digitalmars-d mailing list