[Article Contest, first draft] D Slices

Alex_Dovhal alex_dovhal at yahoo.com
Wed May 18 13:47:47 PDT 2011


"Steven Schveighoffer" <schveiguy at yahoo.com> ÓÏÏÂÝÉÌ/ÓÏÏÂÝÉÌÁ × ÎÏ×ÏÓÔÑÈ 
ÓÌÅÄÕÀÝÅÅ: news:op.vvou3fbgeav7ka at localhost.localdomain...
> Having seen quite a few incorrect descriptions of how D slices work 
> (particularly regarding appending), I wrote an article that tries to 
> describe how D slices work, and why they behave the way they do.
>
> Being one of the only places where I have web space, it's on my 
> dcollections site, I probably will move it to the D wiki eventually, but 
> I'm much more familiar with the Trac wiki syntax.
>
> Please, if you have any comments or recommendations, let me know.  I 
> certainly am no author, so I probably screwed a few things up :)
>
> http://www.dsource.org/projects/dcollections/wiki/ArrayArticle
>
> Thanks
>
> -Steve
>

Nice article, very pleasured to read. One thing that strained my mind - this 
exaple:

>import std.stdio;
>
>char[] fillAs(char[] buf, size_t num)
>{
>   if(buf.length < num)
>      buf.length = num; // increase buffer length to be able to hold the 
> A's
>   buf[0..num] = 'A';   // assign A to all the elements
>   return buf[0..num];  // return the result
>}
>
>void main()
>{
>   char[] str = new char[10];   str[] = 'B';
>   fillAs(str, 20);     // buffer must be reallocated
>   writeln(str);        // "BBBBBBBBBB"
(1)
>   fillAs(str, 12);     // buffer can be extended in place!
>   writeln(str);        // "AAAAAAAAAA";
>}

Please add in (1) assert(str.capacity == 15);






More information about the Digitalmars-d mailing list