Array literals MUST be immutable.

Denis Koroskin 2korden at gmail.com
Wed Feb 17 01:37:08 PST 2010


On Wed, 17 Feb 2010 12:17:09 +0300, Walter Bright  
<newshound1 at digitalmars.com> wrote:

> Don wrote:
>> This is for me the last remaining D2 issue.
>
> That would make it difficult to do things like:
>
>    int*[] foo(int *p)
>    {
>      return [p, p + 1];
>    }
>
> as all the elements of the literal would also have to be immutable. I  
> think you've made a good case, but there is also this issue.

IIRC, it was discussed before and the following solution was suggested:

T[] toArray(T)(T[] values...)
{
     return values.dup; // not sure why dup is needed here, but DMD2.039  
complains about escaping reference
}

int main()
{
     int* p = null;
     int*[] array = toArray(p, p + 1);
     assert(array.length == 2);
     assert(array[0] is p);
     assert(array[1] is (p + 1));

     return 0;
}

In my opinion, this is a minor use case that has graceful solution, in no  
way it is a show stopper.



More information about the Digitalmars-d mailing list