www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Difference between slice[] and slice

reply WhatMeWorry <kheaser gmail.com> writes:
Just got through debugging a line of code which uses dynamic 
array.  It boiled to to my use of [].  How should I "D think" 
about slice[]?  The run time error seems to say the the length of 
[] is zero.   I was assuming that [] meant "the entirety" of the 
array.

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

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

     waste = waste[0..$-1]; // works
Sep 25 2019
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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
Sep 25 2019
parent reply WhatMeWorry <kheaser gmail.com> writes:
On Wednesday, 25 September 2019 at 19:25:06 UTC, Ali Çehreli 
wrote:
 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.
Ok. But which element(s)? In my specific case, I was using []. Is waste[] = waste[0..$-1]; even semantically meaningful? Because the LDC compiler had no problem compiling it.
 //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
Sep 25 2019
next sibling parent Paul Backus <snarwin gmail.com> writes:
On Wednesday, 25 September 2019 at 20:36:47 UTC, WhatMeWorry 
wrote:
 Ok.  But which element(s)?  In my specific case, I was using 
 []. Is

 waste[] = waste[0..$-1];

 even semantically meaningful?  Because the LDC compiler had no 
 problem compiling it.
`waste[]` is just shorthand for `waste[0..$]`. Assigning to a slice means copying the contents of another array into the array that slice refers to. If the lengths of the source and destination don't match, you get an error. Since `waste[0..$]` and `waste[0..$-1]` can never have the same length, you will always get an error if you try to assign one to the other. Source: https://dlang.org/spec/arrays.html#array-copying
Sep 25 2019
prev sibling parent ag0aep6g <anonymous example.com> writes:
On 25.09.19 22:36, WhatMeWorry wrote:
 On Wednesday, 25 September 2019 at 19:25:06 UTC, Ali Çehreli wrote:
 On 09/25/2019 12:06 PM, WhatMeWorry wrote:
[...]
 In short, is there anytime that one would want to use
"slice[] =
 something" syntax?I
That changes element values.
Ok.  But which element(s)?
All of them. For example, `slice[] = 42;` sets all elements to 42. And `slice[] = another_slice[];` replaces all elements of `slice` with copies of `another_slice`'s elements.
  In my specific case, I was using []. Is
 
 waste[] = waste[0..$-1];
 
 even semantically meaningful?  Because the LDC compiler had no problem 
 compiling it.
It's equivalent to this: ---- waste[0] = waste[0..$-1][0]; waste[1] = waste[0..$-1][1]; ... waste[waste.length - 2] = waste[0..$-1][waste.length - 2]; waste[waste.length - 1] = waste[0..$-1][waste.length - 1]; ---- So it basically does nothing. It just copies `waste`'s elements over themselves. Except that the last line makes an out-of-bounds access. That's an error that may be detected during compilation or at run time. Or if you're telling the compiler to optimize too aggressively, it might go unnoticed.
Sep 25 2019