array slicing currently does not support steps?

jmh530 john.michael.hall at gmail.com
Tue Aug 4 16:03:32 UTC 2020


On Tuesday, 4 August 2020 at 14:52:49 UTC, Paul Backus wrote:
> [snip]
>
> Currently, when you do `y = x[0..5]`, both x and y point to the 
> same memory.
>
> In order for `y == [1, 3, 5]` to be true, one of the following 
> must hold:
>
> 1) y points to memory which contains 1, 3, and 5 in contiguous 
> locations.
> 2) y has an overloaded == operator which takes the stride into 
> account.
>
> If #1 is true, then y cannot point to the same memory as x, 
> which means a copy has to be made. If #2 is true, then y cannot 
> be a simple int[], but must instead be a custom type that 
> stores the stride in addition to a pointer and a length.
>
> As WebFreak001 pointed out, you can get the "custom type" 
> version with `x[0..5].stride(2)`, and the the "array with 
> copied elements" version with `x[0..5].stride(2).array`.

Thanks, that makes sense.


More information about the Digitalmars-d mailing list