opNext: Simplified ranges
Dukc
ajieskola at gmail.com
Sat Nov 5 21:16:26 UTC 2022
On Saturday, 5 November 2022 at 18:08:39 UTC, Paul Backus wrote:
> On Saturday, 5 November 2022 at 15:17:42 UTC, Steven
> Schveighoffer wrote:
>> This can be solved by a wrapper:
>
> You'd think, but if you actually try this, it turns out it
> doesn't work: the compiler is "smart" enough to see through the
> `alias this`, and makes a copy of `range.refOf._get` (the
> actual range object) instead of `range.refOf` (the wrapper).
This works:
```D
void main()
{ import std.range : inputRangeObject, refRange;
import std.stdio;
auto testPiece = [1,2,3,4,5];
foreach(el; (&testPiece).refRange.inputRangeObject)
{ if(el == 3) break;
}
writeln(testPiece); // [3, 4, 5]
}
```
It's far from satisfying though. Ugly to begin with, and worse,
incompatible with any function attributes.
More information about the Digitalmars-d
mailing list