[Article Contest, first draft] D Slices

Steven Schveighoffer schveiguy at yahoo.com
Mon May 23 04:05:12 PDT 2011


On Sun, 22 May 2011 22:46:13 -0400, Jonathan M Davis <jmdavisProg at gmx.com>  
wrote:

> On 2011-05-18 11:03, Steven Schveighoffer wrote:
>> 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
>
> Finally got around to reading it. Definitely a good article. I even  
> learned a
> few things from it (primarily surrounding implementation details). I am  
> a bit
> surprised that you never mentioned that a slice is essentially struct  
> with the
> a C style array pointer and the length of the slice (particularly since  
> that
> helps to explain why passing an array without ref means that altering the
> slice rather than its elements doesn't alter the array that was passed  
> in),
> but it still works quite well without that.

A slice is kind of an opaque type.  You shouldn't rely on the exact  
implementation (at one point, Walter was considering changing it to a  
begin and end pointer), although I do describe its implementation, without  
saying it's a struct:

"A slice is a segment of an array (dynamic or otherwise) that tracks both  
the pointer and the length of the segment"

> A couple of total nitpicks would
> be that the first sentence should say "...is its implementation..."  
> rather
> than using "are",

Fixed, thanks!

> and the title "A Slice You Can Append On" should be "A Slice
> You Can Append To."

This was intentional :)  It's a pun (something "you can depend on").

> But as those just nitpicky grammar issues. Overall, it's
> very good and definitely something that D programmers (especially  
> newbies)
> should read. Dynamic arrays and slices throw them off too often.

Thanks!

-Steve


More information about the Digitalmars-d mailing list