[Article Contest, first draft] D Slices

Steven Schveighoffer schveiguy at yahoo.com
Wed May 18 13:54:27 PDT 2011


On Wed, 18 May 2011 16:37:49 -0400, Jesse Phillips  
<jessekphillips+D at gmail.com> wrote:

> 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.
>
> Still reading, but the example should use assertions which pass:
>
> void main()
> {
>    int[] arr = new int[5];
>    shrinkTo2(arr);
>    assert(arr.length != 2); // Not expected!
> }
>
> It is good form because reading it will describe the outcome even  
> without comment.

I always wonder about that.  One of the issues with assert for people  
"feeling" out the language is, a passing assert doesn't seem to do  
anything.

For instance, in this example, if you take the code I wrote, and compile  
it, you'll get a loud assertion error (proving the assertion runs and  
fails).

If you take your code and run it, you get a command prompt.  It doesn't  
really help you see how it works.

I'm contemplating switching it to a writeln instead ;)

-Steve


More information about the Digitalmars-d mailing list