phobos icon indicating copy to clipboard operation
phobos copied to clipboard

Problem with a map() of a const uniq()

Open dlangBugzillaToGithub opened this issue 14 years ago • 2 comments

bearophile_hugs reported this on 2011-02-14T11:03:24Z

Transferred from https://issues.dlang.org/show_bug.cgi?id=5575

Description

D2 code. map() doesn't modify the input range, so I think this is correct (but I am not sure if it's correct. A mutable range of immutable items isn't the same thing as an immutable range):


import std.algorithm;
void main() {
    auto u1 = uniq([1]);
    auto m1 = map!q{ a }(u1); // OK
    const u2 = u1;
    auto m2 = map!q{ a }(u2); // Error
}


DMD 2.051 gives:

...\dmd\src\phobos\std\algorithm.d(109): Error: constructor std.algorithm.Map!(result,const(Uniq!(pred,int[]))).Map.this (Uniq!(pred,int[]) input) is not callable using argument types (const(Uniq!(pred,int[])))
...\dmd\src\phobos\std\algorithm.d(109): Error: cannot implicitly convert expression (r) of type const(Uniq!(pred,int[])) to Uniq!(pred,int[])
test4.d(6): Error: template instance std.algorithm.map!(" a ").map!(const(Uniq!(pred,int[]))) error instantiating

dlangBugzillaToGithub avatar Feb 14 '11 11:02 dlangBugzillaToGithub

bearophile_hugs commented on 2012-02-03T15:41:44Z

This seems related, DMD 2.058head:


import std.algorithm, std.array;
void main() {
    auto s1 = "hello";
    auto m1 = map!(c => c)(s1);
    auto a1 = array(m1); // OK
    auto s2 = "hello"d;
    auto m2 = map!(c => c)(s2);
    auto a2 = array(m2); // line 8, Error
}


...\dmd2\src\phobos\std\array.d(55): Error: result[i] isn't mutable
test.d(8): Error: template instance std.array.array!(Result) error instantiating

dlangBugzillaToGithub avatar Feb 03 '12 15:02 dlangBugzillaToGithub

This already works for many ranges where the following assignment works

const constRange = Somerange();
alias R = Unqual(typeof(r));
R r = constRage;

It fails for some types containing pointers, as it should, as these don't have value-semantics anymore.

Consider this range:

struct S {
  int[] source;
  int i;
  int* front() => (source.ptr + i);
  void popFront() {
    i++;
  }
  bool empty() => i >= source.length;
}

It would be unwise to automatically cast away constness from a range like this.

A version of Unqual that leaves constness of pointed to data intact would resolve.

Inkrementator avatar Nov 11 '25 18:11 Inkrementator