www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Whats the proper way to write a Range next function

reply =?UTF-8?Q?Christian_K=c3=b6stlin?= <christian.koestlin gmail.com> writes:
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/v
ctor/package.d#L229 
is looks like there should be tons of annotations/attributes on it.

Kind regards,
Christian
Jun 15 2022
next sibling parent Dennis <dkorpel gmail.com> writes:
On Wednesday, 15 June 2022 at 13:52:24 UTC, Christian Köstlin 
wrote:
 looks like there should be tons of annotations/attributes on it.
Because you have a template function, most attributes will be inferred based on the Range type. `const` is not inferred, but `popFront` mutates so it doesn't apply in your case. ` property` is not inferred either, but the spec says "Using property functions is not recommended until the definition is more certain and implementation more mature."
Jun 15 2022
prev sibling parent reply JG <someone simewhere.com> writes:
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/v
ctor/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.
Jun 15 2022
parent reply JG <someone somewhere.com> writes:
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/v
ctor/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; } ```
Jun 15 2022
parent =?UTF-8?Q?Christian_K=c3=b6stlin?= <christian.koestlin gmail.com> writes:
On 2022-06-15 19:36, JG wrote:
 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/v
ctor/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; } ```
Thanks for the explanation! I actually want to mutate the original range and I know, that there are at least 2 elements in my range. e.g. the program ```d import std.stdio; import std.range; auto next(Range)(Range r) { r.popFront; return r.front; } int main(string[] args) { auto i = iota(10); writeln(i.next); writeln(i.next); return 0; } ``` prints: ``` 1 1 ``` whereas ```d import std.stdio; import std.range; auto next(Range)(ref Range r) { r.popFront; return r.front; } int main(string[] args) { auto i = iota(10); writeln(i.next); writeln(i.next); return 0; } ``` prints ``` 1 2 ``` which is what I would want. So it depends if the Range is a value or reference type ...
Jun 15 2022