www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 10960] New: Copying a const value type should yield unqual

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10960

           Summary: Copying a const value type should yield unqual
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: hsteoh quickfur.ath.cx


--- Comment #0 from hsteoh quickfur.ath.cx 2013-09-03 16:09:25 PDT ---
This is from a forum post.

Code:
------
void main(){
    auto A = [0, 1, 2];
    const B = [10, -20, 30];
    schwartzSort!(i => B[i])(A); // NG
}
------
This fails because the return type of the lambda is deduced as const(int). This
is silly since a copied const value type no longer needs to be const; it should
be unqual.

Explicitly naming the unqual type for the delegate fixes the problem:

    schwartzSort!(delegate int (i) => B[i])(A); // OK

The user shouldn't be required to do this; returning a const value type by
value should be stripped of its constness, since it is not the original value
being returned, but a copy.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 03 2013
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10960



--- Comment #1 from bearophile_hugs eml.cc 2013-09-03 16:35:12 PDT ---
(In reply to comment #0)

 returning a const value type by value should be stripped of
 its constness, since it is not the original value
 being returned, but a copy.
The main point of my question in D.learn was: if the array A is mutable, and the array B contains constant references (like const references to class instances), is code like this acceptable? schwartzSort!(i => B[i])(A); -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Sep 03 2013
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10960



--- Comment #2 from hsteoh quickfur.ath.cx 2013-09-03 16:56:05 PDT ---
Oh I see. I misunderstood your question, my bad. :)

Nevertheless, both issues are worth consideration. The unqual issue should be
fixed, definitely.

About your actual question: I'm not so sure this is possible, although in
theory, it *could* be made possible if B's elements are rebindable. So in
theory, this should work:

    class C { ... }
    auto A = [0, 1, 2];
    const B = [new C(1), new C(2), new C(3)];
    schwartzSort!(i => B[i])(A);

The idea being that schwartzSort stores an array of rebindable references to
the C instances, and uses that to sort A. This probably doesn't work right now
because the language currently has no way to express tail-const in class
references, though I'm guessing that if you use Rebindable!C, it should work.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 03 2013