Whats the proper way to write a Range next function

JG someone at simewhere.com
Wed Jun 15 17:30:31 UTC 2022


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.


More information about the Digitalmars-d-learn mailing list