digitalmars.D.learn - Understanding range.dropBackOne
- Tim (12/12) Sep 28 2021 I'm doing the following:
- Adam Ruppe (12/18) Sep 28 2021 Here the window[] takes a variable-length slice of it. Turning it
- Tim (2/18) Sep 28 2021 Perfect answer. Thanks for clearing that all up mate
I'm doing the following: ```D int[25] window = 0; // Later in a loop window = someInteger ~ window[].dropBackOne; ``` But I'm struggling to understand why the following doesn't work ```D window = someInteger ~ window.dropBackOne; ``` What does the `[]` do exactly? Is an array not a bidirectional range?
Sep 28 2021
On Tuesday, 28 September 2021 at 22:56:17 UTC, Tim wrote:I'm doing the following: int[25] window = 0;Note that this array has a fixed size.window = someInteger ~ window[].dropBackOne;Here the window[] takes a variable-length slice of it. Turning it from int[25] into plain int[]. Then you can drop one since it is variable length. Then appending again ok cuz it is variable. Then the window assign just copies it out of the newly allocated variable back into the static length array.window = someInteger ~ window.dropBackOne;But over here you are trying to use the static array directly which again has fixed length, so it is impossible to cut an item off it or add another to it.What does the `[]` do exactly? Is an array not a bidirectional range?It takes a slice of the static array - fetching the pointer and length into runtime variables.
Sep 28 2021
On Tuesday, 28 September 2021 at 23:12:14 UTC, Adam Ruppe wrote:On Tuesday, 28 September 2021 at 22:56:17 UTC, Tim wrote:Perfect answer. Thanks for clearing that all up mate[...]Note that this array has a fixed size.[...]Here the window[] takes a variable-length slice of it. Turning it from int[25] into plain int[]. Then you can drop one since it is variable length. Then appending again ok cuz it is variable. Then the window assign just copies it out of the newly allocated variable back into the static length array.[...]But over here you are trying to use the static array directly which again has fixed length, so it is impossible to cut an item off it or add another to it.[...]It takes a slice of the static array - fetching the pointer and length into runtime variables.
Sep 28 2021