Debug help - Ranges - Programming in D page 598
monkyyy
crazymonkyyy at gmail.com
Thu Aug 28 14:15:54 UTC 2025
On Thursday, 28 August 2025 at 11:54:10 UTC, Brother Bill wrote:
> If line 9 of the program is commented out, it runs fine.
> Otherwise lots of error messages which don't pan out for me.
> I tried changing Negative.front to be const, which didn't help.
> I am not quite sure what the error messages are telling me.
>
> Thank you for your assistance!
>
> ```
> C:\D\dmd2\windows\bin64\..\..\src\phobos\std\range\package.d(4560): Error: mutable method `app.Negative!(Take!(FibonacciSeries)).Negative.front` is not callable using a `const` object
> return _current.front;
> ^
> c:\dev\D\81 -
> 90\c82_1e_InputRange_does_support_cycle_with_save\source\app.d(44): Consider adding `const` or `inout` here
> auto front()
> ^
> C:\D\dmd2\windows\bin64\..\..\src\phobos\std\range\package.d(4695): Error: template instance `std.range.Cycle!(Negative!(Take!(FibonacciSeries)))` error instantiating
> else return Cycle!R(input);
> ^
> c:\dev\D\81 -
> 90\c82_1e_InputRange_does_support_cycle_with_save\source\app.d(9): instantiated from here: `cycle!(Negative!(Take!(FibonacciSeries)))`
> .cycle // ← compilation ERROR
> ^
> ```
>
> source/app.d
> ```
> import std.stdio;
> import std.range;
>
> void main()
> {
> writeln(FibonacciSeries()
> .take(5)
> .negative
> .cycle // ← compilation ERROR
> .take(10));
> }
>
> struct FibonacciSeries
> {
> int current = 0;
> int next = 1;
> enum empty = false;
> int front() const
> {
> return current;
> }
>
> void popFront()
> {
> const nextNext = current + next;
> current = next;
> next = nextNext;
> }
>
> FibonacciSeries save() const
> {
> return this;
> }
> }
>
> struct Negative(T) if (isInputRange!T)
> {
> T range;
> bool empty()
> {
> return range.empty;
> }
>
> auto front()
> {
> return -range.front;
> }
>
> void popFront()
> {
> range.popFront();
> }
>
> static if (isForwardRange!T)
> {
> Negative save()
> {
> return Negative(range.save);
> }
> }
> }
>
> Negative!T negative(T)(T range)
> {
> return Negative!T(range);
> }
> ```
add it to the pile of phoboes safetyism breaking code; also see,
nullable is 1000 lines long, breaks naive type inference when a 5
line version often outcompetes it
cycle can be defined like this:
```d
void main()
{
writeln(FibonacciSeries()
.take(5)
.negative
.mycycle // ← compilation ERROR
.take(10));
}
auto mycycle(R)(R r){
struct cycle{
R r;
R backup;
auto front()=>r.front;
void popFront(){
r.popFront();
if(r.empty){r=backup;}
}
enum empty=false;
}
return cycle(r,r);
}
```
phoboes cycle has a bunch of overloads a type infomation, so Id
bet theres a confused "ElementType" inferring const (even if you
dont add it it may be *incorrectly* inferred, safetism is just
function coloring bullshit) in its dense web
for example:
https://github.com/dlang/phobos/blob/205256abb1f86faf986f8c789cb733ca4137246e/std/range/package.d#L4621
```d
@property ref inout(ElementType) front() inout @safe
{
static ref auto trustedPtrIdx(typeof(_ptr) p, size_t idx)
@trusted
{
return p[idx];
}
return trustedPtrIdx(_ptr, _index);
}
```
they added an overload that handles "random access ranges"
differently(so this isnt the only one of these); they added at
least 4 safetism attributes, added a static function and 8 lines
of code that only does `ref front()=>r.front;`
They lose the plot and you have to sometimes offer your own
solutions, its often not worth debugging, if you see an ugly
phoboes error on what should be a simple range concept, consider
not even looking.
More information about the Digitalmars-d-learn
mailing list