Ranges

frame frame86 at live.com
Thu Aug 4 18:05:22 UTC 2022


On Thursday, 4 August 2022 at 13:08:21 UTC, pascal111 wrote:

> 1) Why the programmer needs to program "empty()", "front()", 
> and "popFront()" functions for ranges while they exist in the 
> language library? it seems there's no need to exert efforts for 
> that. "https://dlang.org/phobos/std_range_primitives.html"

- These functions are wrappers to use something as range
- Ranges need to implement the functions to keep their data 
private, also there are complex types the need to handle data 
differently
- Ranges must implement the functions so other function can 
recognize it as such (eg. `isInputRange`) - there is no common 
interface, it's determined by compile time

> 2) "front()", and "popFront()" are using fixed constants to 
> move forward the range, while they should use variables.

`front()` is always using the first element BUT `popFront()` 
copies all elements except the first one into the variable (and 
overwrites it), so it moves the data forward.


More information about the Digitalmars-d-learn mailing list