[Article Contest, first draft] D Slices
Steven Schveighoffer
schveiguy at yahoo.com
Wed May 18 13:57:06 PDT 2011
On Wed, 18 May 2011 16:47:47 -0400, Alex_Dovhal <alex_dovhal at yahoo.com>
wrote:
> 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);
hm... I think I'll add a comment instead, since I haven't discussed the
capacity property yet. People might not get what that means.
Thanks for the suggestion!
-Steve
More information about the Digitalmars-d
mailing list