www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Understanding range.dropBackOne

reply Tim <tim.oliver tutanota.com> writes:
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
parent reply Adam Ruppe <destructionator gmail.com> writes:
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
parent Tim <tim.oliver tutanota.com> writes:
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:
 [...]
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.
Perfect answer. Thanks for clearing that all up mate
Sep 28 2021