Thread overview
Understanding range.dropBackOne
Sep 28, 2021
Tim
Sep 28, 2021
Adam Ruppe
Sep 28, 2021
Tim
September 28, 2021

I'm doing the following:

int[25] window = 0;

// Later in a loop
window = someInteger ~ window[].dropBackOne;

But I'm struggling to understand why the following doesn't work

window = someInteger ~ window.dropBackOne;

What does the [] do exactly? Is an array not a bidirectional range?

September 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.

September 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:

>

[...]

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