Range Redesign: Empty Ranges
Paul Backus
snarwin at gmail.com
Wed Mar 6 17:43:05 UTC 2024
On Wednesday, 6 March 2024 at 17:32:17 UTC, H. S. Teoh wrote:
> Every time this topic comes up, class-based ranges become the
> whipping boy of range design woes. Actually, they serve an
> extremely important role: type erasure, which is critical when
> you have code like this:
>
> auto myRangeFunc(R,S)(R range1, S range2) {
> if (runtimeDecision()) {
> return range1;
> } else {
> return range2;
> }
> }
>
> [...]
>
> Note that you can't use a struct wrapper here, because R and S
> have different ABIs; the only way to correctly forward range
> methods to R or S is to use overridden base class methods. IOW,
> the type erasure of R and S is unavoidable for this code to
> work.
This is what std.range.choose [1] is for. Internally, it's
implemented as a tagged union of R and S.
[1] https://dlang.org/library/std/range/choose.html
More information about the Digitalmars-d
mailing list