Dynamic arrays in D 1.0

Bill Baxter dnewsgroup at billbaxter.com
Mon May 12 22:05:15 PDT 2008


Edward Diener wrote:
> boyd wrote:
>> I think it's because erase and insert are really just the inverse of 
>> slicing and concatenation.
>>
>> int[] a, c;
>>
>> // insert
>> c = a.insert(b, 10);
>> c = a[0..10] ~ b ~ a[10..$];
>>
>> // delete
>> c = a.delete(&a[10..20]);
>> c = a[0..10] ~ a[20..$];
>>
>> I essence delete and insert are just another way of expressing slices 
>> and concatenations. Personally I prefer slicing and concatting stuff, 
>> because it expresses better what's actually happening.
>>
>> Delete and Insert on the other hand are higher level and generally 
>> more intuitive. They leave optimization to the compiler/library. And I 
>> think most D-users like more control of optimizations than users of 
>> more high level languages.
> 
> You make a good point that things like delete, insert, and replace can 
> be done with slicing and concatentaion. Thanks for bringing up these 
> solutions. 

Part of the comment, though, was that delete/insert are "higher-level 
and generally more intuitive".  With that I agree.

 > Having used Python extensively, where slicing is also a big
> part of lists, I should have realized that such operations are largely 
> subsumed by slicing and concatenation in D. I have to take my C++ hat 
> off and put my Python hat one when dealing with dynamic arrays in D.

Fine point, except Python lists also have append, extend, index, insert, 
pop, and remove methods which you can use when they express your intent 
better than fidgety slicing expressions.

--bb



More information about the Digitalmars-d mailing list