how do I implement opSlice for retro range?

Jack jckj33 at gmail.com
Fri May 14 16:41:45 UTC 2021


On Friday, 14 May 2021 at 14:14:03 UTC, Steven Schveighoffer 
wrote:
> On 5/13/21 11:49 PM, Jack wrote:
>> [...]
>
> Just slice the `a`, appropriately. You have to translate the 
> indexes back into the original array.
>
> ```d
> auto opSlice(size_t start, size_t end)
> {
>   return typeof(this)(a[$ - end .. $ - start]);
> }
> ```
>
> You should also define `length`, `save`, `opIndex`, and 
> `opDollar` so that it fits in the range hierarchy as a proper 
> random-access range.
>
> But I question whether you shouldn't just use `std.range.retro` 
> directly? It does all this for you:
>
> ```d
> // inside A
> auto retro() {
>    import std.range : retro;
>    return arr.retro;
> }
> ```
>
> -Steve

much easier, I'll be just using the one from std.range. Thanks!


More information about the Digitalmars-d-learn mailing list