Difference between slice[] and slice

Ali Çehreli acehreli at yahoo.com
Wed Sep 25 19:25:06 UTC 2019


On 09/25/2019 12:06 PM, WhatMeWorry wrote:

 > I was
 > assuming that [] meant "the entirety" of the array.

Assuming we're talking about D slices, Yes. (It could be a user-defined 
type with surprisingly different semantics.)

 > In short, is there anytime that one would want to use "slice[] =
 > something" syntax?I

That changes element values.

 > //waste[] = waste[0..$-1]; // object.Error@(0): Array lengths don't
 > match for copy: 0 != 1
 >
 >      waste = waste[0..$-1]; // works

That makes slice refer to a different set of elements. In that example, 
the slice does not include the last element anymore.

Ali



More information about the Digitalmars-d-learn mailing list