Thread overview
Debug help - Ranges - Programming in D page 598
Aug 28
monkyyy
Aug 28
monkyyy
August 28

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);
}
August 28

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:

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

    @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.

August 28

On Thursday, 28 August 2025 at 14:15:54 UTC, monkyyy wrote:

>

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.

Although at least consider submitting a suggestion on a better error message. It's clear the ldc2 compiler has evolved towards being much more helpful; perhaps the Phobos team will file off some rough edges based on user experience.

Andy

August 28

On Thursday, 28 August 2025 at 15:21:41 UTC, Andy Valencia wrote:

>

On Thursday, 28 August 2025 at 14:15:54 UTC, monkyyy wrote:

>

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.

Although at least consider submitting a suggestion on a better error message.

Its wrong that op's code fails. That op has a reasonable range and cycle isnt that hard for a naive implementation. (tho someone will likely rant at me about .save)

If I can write 10 lines replacement code where a 1000 lines of code doesn't work, something with your 1000 lines of code is wrong.

>

It's clear the ldc2 compiler has evolved towards being much more helpful; perhaps the Phobos team will file off some rough edges based on user experience.

Andy

My suggestion for better template hell error messages is here; walter did not get it:

https://forum.dlang.org/thread/fwxpamdmlrmoqtkazupn@forum.dlang.org

and look at that, cycle implements template landmines: https://github.com/dlang/phobos/blob/205256abb1f86faf986f8c789cb733ca4137246e/std/range/package.d#L4687

Such things are trade offs, maybe you correctly inferred all types correctly(but clearly not in practice, see above) but if you ever pattern match into a static assert; less code works. Hot take: writing code that makes less code work is a bad use of code writing time.

>

perhaps the Phobos team will file off some rough edges based on user experience.

Phoboes is far past the point where further complexity tradeoffs make sense even by their estimation.

August 29

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
            ^

This looks like a bug in the library or the compiler.

This code is the code that is failing:

        static if (is(typeof((cast(const R)_current).front)))
        {
            /// ditto
            @property auto ref front() const
            {
                return _current.front;
            }
        }

What the intent looks to me like is, if _current.front can be called on a const object, then we can forward the const-ness by declaring a const-allowing front function.

Well, looking at the Negative.front function, I see no attribute of const.

There is something sutble here as well. is(typeof(funcname)) does not check if funcname is valid as an expression, but as a symbol.

And part of this also is that you are forming an rvalue, which has different implications for pure value types (which this whole thing is).

If I change that test to static if (is(typeof((cast(const R*)&_current).front))) then the code works. But I'm not sure if there's still not a compiler bug somewhere.

Will file an issue, and we should get this fixed.

-Steve

August 29

On Friday, 29 August 2025 at 01:24:40 UTC, Steven Schveighoffer wrote:

>

Will file an issue, and we should get this fixed.

https://github.com/dlang/phobos/issues/10852

-Steve

5 days ago

On Friday, 29 August 2025 at 01:51:27 UTC, Steven Schveighoffer wrote:

>

On Friday, 29 August 2025 at 01:24:40 UTC, Steven Schveighoffer wrote:

>

Will file an issue, and we should get this fixed.

https://github.com/dlang/phobos/issues/10852

Fixed!

https://github.com/dlang/phobos/pull/10854

Will be probably in next release.

-Steve