Eliminate redundancy of dup/idup

kenji hara k.hara.pg at gmail.com
Sun Sep 9 10:42:32 PDT 2012


2012/9/10 Timon Gehr <timon.gehr at gmx.ch>:
> On 09/09/2012 07:06 PM, kenji hara wrote:
>>
>> An advantage of generic dup() is to allow copying of const arrays.
>>
>> void main()
>> {
>>      class C {}
>>      const C[] ca = new C[3];
>>      //ca.dup;
>>      // Error: cannot implicitly convert element type const(C) to
>> mutable in ca.dup
>>      //ca.idup;
>>      // Error: cannot implicitly convert element type const(C) to
>> immutable in ca.idup
>>      ca.dup();
>>      // result is const(C[])
>> }
>>
>> ----
>>
>> According to what I remember, the name 'idup' had been discussed in
>> the beginning of D2, but it had been disappeared from the need for
>> idup. But it is still oddly name.
>>
>> Using:
>>    auto iarr = marr.idup;
>> Instead of :
>>    immutable iarr = marr.dup();  // In the future, () would not be
>> necessary
>>
>> Is really handy? Yes, it is 6(or 4) characters shorter.  But I can't
>> see it *handy*.
>
>
> What you are missing is that the two code segments are not equivalent.

Hmm, it's right. They are not equivalent.

void main()
{
    int[] marr;
    auto iarr1 = marr.idup;
    immutable iarr2 = marr.dup();
    pragma(msg, typeof(iarr1)); // prints immutable(int)[]
    pragma(msg, typeof(iarr2)); // prints immutable(int[])
}

It's hard problem...

Kenji Hara


More information about the Digitalmars-d mailing list