Thread overview
[Issue 6289] New: Make slices of const/immutable arrays mutable (but keep the elements const/immutable)
Jul 12, 2011
Jonathan M Davis
Jul 12, 2011
yebblies
Oct 08, 2011
Walter Bright
July 12, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6289

           Summary: Make slices of const/immutable arrays mutable (but
                    keep the elements const/immutable)
           Product: D
           Version: unspecified
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: jmdavisProg@gmx.com


--- Comment #0 from Jonathan M Davis <jmdavisProg@gmx.com> 2011-07-11 17:12:35 PDT ---
Okay. If you have

immutable val = [1, 2, 3, 4];
void func(immutable(int)[] arg) {}

you can pass val to func even though val is fully immutable. The compiler realizes that it's safe, because func cannot alter the original array. The array in the function is a slice of the original and can't affect the original (since the elements are immutable). However, even though this is true, the type of a slice of an immutable int[], is still immutable int[], not immutable(int)[] as with the function parameter. I'd like to see

immutable val = [1, 2, 3, 4];
assert(is(typeof(val) == immutable(int[])));
assert(is(typeof(val[]) == immutable(int)[]));

The slice is then mutable (though its elements are not) - which makes sense, since that's exactly what the function described above does. The reason that I want this to be the case is that you can then use immutable arrays with range-based functions if you slice them (just as is the case with static arrays). As it stands, both

find(val, 3);

and

find(val[], 3);

are illegal, because the compiler tries to instantiate find with immutable(int[]). But if the type of a slice of val were immutable(int)[], then the second find call would work. This would help solve the problem of immutable arrays not working with range-based functions (see bug# 6148). Then, if you slice static arrays and immutable/const arrays (just as you'd have to do with container), they will work with range-based functions. As it stands, that works with static arrays but not immutable or const arrays.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
July 12, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6289


yebblies <yebblies@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch
                 CC|                            |yebblies@gmail.com


--- Comment #1 from yebblies <yebblies@gmail.com> 2011-07-12 16:41:31 EST ---
https://github.com/D-Programming-Language/dmd/pull/236

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 08, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6289


Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |bugzilla@digitalmars.com
         Resolution|                            |FIXED


--- Comment #2 from Walter Bright <bugzilla@digitalmars.com> 2011-10-08 14:01:49 PDT ---
https://github.com/D-Programming-Language/dmd/commit/55288ab7455cbdaf0bedccb0d054d2ae6ba453dd

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