Comment #0 by jlourenco5691 — 2021-09-03T19:48:47Z
The Range retro follows the current Range hierarchy. This brings friction when working with this range because it forces the user to implement a 'save' method. Iterating a retro range should be the same as iterating with a foreach_reverse. The foreach_reverse allows iterating ranges without the 'save' method implementation. I would even argue that 'popFront' and 'front' shouldn't be required because once again foreach_reverse allows that.
---
struct Foo
{
void popBack() { i--; }
bool empty() { return !i; }
auto back() { return i; }
int i = 1;
}
void main()
{
foreach_reverse(_; Foo.init) {}
}
---
Comment #1 by dlang-bot — 2021-09-03T20:27:49Z
@iK4tsu created dlang/phobos pull request #8231 "Fix std.range.retro not behaving the same as foreach_reverse" fixing this issue:
- std.range: fix function 'save' to only be implemented if the Range is has a valid 'save' method
The 'retro' Range only does one thing, iterates a Range back to front. This
follows the same semantics as a 'foreach_reverse'. However, with 'foreach_reverse'
it accepts Ranges that have valid 'popBack', 'back', and 'empty' functions. The
'retro' Range, on the other hand, forces the user to implement 'front',
'popFront', and 'save' functions as well because it follows the Range hierarchy.
For a Range that only iterates back to front, accomplishing the same as the
'foreach_reverse', it should behave the same way.
Fixes Issue #22272
Signed-off-by: João Lourenço <[email protected]>
https://github.com/dlang/phobos/pull/8231
Comment #2 by robert.schadek — 2024-12-01T16:39:12Z