www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 20671] New: T[] can't implicitly convert to immutable(T)[]

https://issues.dlang.org/show_bug.cgi?id=20671

          Issue ID: 20671
           Summary: T[] can't implicitly convert to immutable(T)[] even if
                    T is de-facto immutable
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: default_357-line yahoo.de

Consider a struct S:

struct S { immutable int i; }

Even though it's nominally mutable, it contains no mutable fields.

However, if we have a "mutable" array:

S[] array = [S(5)];

We cannot convert this array to immutable(S)[]

immutable(S)[] array2 = array; // errors

Even though this is clearly safe, because S has no fields that could mutate or
be mutated, whether you write `S` or `immutable(S)`.

This combines with https://issues.dlang.org/show_bug.cgi?id=20670 to make
immutable arrays effectively unusable as struct members.

--
Mar 13 2020