digitalmars.D.learn - Difference between slice[] and slice
- WhatMeWorry (10/10) Sep 25 2019 Just got through debugging a line of code which uses dynamic
- =?UTF-8?Q?Ali_=c3=87ehreli?= (7/14) Sep 25 2019 Assuming we're talking about D slices, Yes. (It could be a user-defined
- WhatMeWorry (7/24) Sep 25 2019 Ok. But which element(s)? In my specific case, I was using [].
- Paul Backus (9/14) Sep 25 2019 `waste[]` is just shorthand for `waste[0..$]`. Assigning to a
- ag0aep6g (18/33) Sep 25 2019 All of them. For example, `slice[] = 42;` sets all elements to 42. And
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
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?IThat changes element values.//waste[] = waste[0..$-1]; // object.Error (0): Array lengths don't match for copy: 0 != 1 waste = waste[0..$-1]; // worksThat 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
On Wednesday, 25 September 2019 at 19:25:06 UTC, Ali Çehreli wrote:On 09/25/2019 12:06 PM, 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.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?IThat changes element values.//waste[] = waste[0..$-1]; // object.Error (0): Array lengthsdon'tmatch for copy: 0 != 1 waste = waste[0..$-1]; // worksThat 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
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
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: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.Ok. But which element(s)?In short, is there anytime that one would want to use"slice[] =something" syntax?IThat changes element values.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