array slicing currently does not support steps?

jmh530 john.michael.hall at gmail.com
Tue Aug 4 13:15:39 UTC 2020


On Tuesday, 4 August 2020 at 07:39:04 UTC, Simen Kjærås wrote:
> [snip]
> Currently, a slice is essentially a (ptr, length) struct, and 
> adding stride to the mix would require adding another field to 
> every slice. This would break all D code ever, so we can 
> disregard that solution.
[snip]

I think I've suggested something similar in the past. I don't 
understand why another field is required as it would just be some 
special syntax over indexing.

For instance, I would imagine it used like
auto x = [1, 2, 3, 4, 5, 6, 7];
auto y = x[0..2..5];
assert(y == [1, 3, 5]);

This would be marginally different from python, which puts the 
strides last and uses colons (which I prefer), but its the same 
general idea (other languages have similar behavior).

A language like Chapel optionally allows for strides (which do 
take up space). A user-defined type in D could do the same thing. 
If the stride is a compile-time type, then you could have a 
specialization where the stride is 1 and does not take up space.





More information about the Digitalmars-d mailing list