Thread overview
[Issue 19981] std.algorithm.iteration.group fails when element type has a const/immutable member
Jun 18, 2019
Vladimir Panteleev
Jun 18, 2019
Vladimir Panteleev
Dec 17, 2022
Iain Buclaw
June 18, 2019
https://issues.dlang.org/show_bug.cgi?id=19981

--- Comment #1 from Vladimir Panteleev <dlang-bugzilla@thecybershadow.net> ---
It doesn't work because Group.front's return value is a ref to a field (_current), however, because the range element type has const members, it cannot be overwritten, i.e. it would break:

auto r=group(...);
auto p = &r.front.x;
auto v = *p; r.popFront;
assert(*p == v, "A const variable has changed");`.

It's not actually a problem from this side with const, but it is with immutable, which exhibits the same issue.

Not sure this is fixable in the general case, since many range algorithms have to store a copy of the range elements in some way. Adding constness to field members implies restricting some operations on them.

This could be worked around by wrapping the actual values with a proxy type which stores a pointer but forwards everything else to the pointer target (perhaps something like this exists in Phobos already):

////////////////////////////////// test.d //////////////////////////////////
import std.algorithm.iteration;
import std.stdio;
import std.typecons;

struct Ref(T)
{
    T* _obj;
    ref inout(T) getObj() inout { return *_obj; }
    alias getObj this;

    bool opEquals(ref Ref other) const { return *other._obj == *this._obj; }
}
Ref!T toRef(T)(ref T v) { return Ref!T(&v); }

void main()
{
    static struct X {
        immutable int x;
    }
    [X(1),X(2),X(2),X(3)]
        .map!((ref x) => toRef(x))
        .group()
        .writeln();
}
////////////////////////////////////////////////////////////////////////////

--
June 18, 2019
https://issues.dlang.org/show_bug.cgi?id=19981

Vladimir Panteleev <dlang-bugzilla@thecybershadow.net> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |dlang-bugzilla@thecybershad
                   |                            |ow.net

--
December 17, 2022
https://issues.dlang.org/show_bug.cgi?id=19981

Iain Buclaw <ibuclaw@gdcproject.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P1                          |P3

--
December 01
https://issues.dlang.org/show_bug.cgi?id=19981

--- Comment #2 from dlangBugzillaToGithub <robert.schadek@posteo.de> ---
THIS ISSUE HAS BEEN MOVED TO GITHUB

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

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB

--