Whats the proper way to write a Range next function
JG
someone at somewhere.com
Wed Jun 15 17:36:09 UTC 2022
On Wednesday, 15 June 2022 at 17:30:31 UTC, JG wrote:
> On Wednesday, 15 June 2022 at 13:52:24 UTC, Christian Köstlin
> wrote:
>> the naive version would look like
>>
>> ```d
>> auto next(Range)(Range r) {
>> r.popFront;
>> return r.front;
>> }
>> ```
>>
>> But looking at a mature library e.g.
>> https://github.com/submada/btl/blob/9cc599fd8495215d346ccd62d6e9f1f7ac140937/source/btl/vector/package.d#L229 is looks like there should be tons of annotations/attributes on it.
>>
>> Kind regards,
>> Christian
>
> I not sure of your use case. But you need to check if the range
> is empty before and after calling popFront (and decide what to
> if it is). Also unless you want the original range passed in
> mutated you should call save (assuming you have a forward
> range) before calling your next or you need to modify next so
> it calls save.
This is what I would write for next (which should only be called
after
checking if the range is empty).
It produces a range whose front points at the next element.
```d
auto next(Range)(Range r) {
auto ret = r.save;
ret.popFront;
return ret;
}
```
More information about the Digitalmars-d-learn
mailing list