Summary: Proposal: implement arr.dup in library
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: druntime

--- Comment #0 from Kenji Hara <> 2012-07-22 05:38:09 PDT ---
If issue 8408 is fixed, we can use following dup function definition.

template hasMutableIndirection(T)
    enum hasMutableIndirection = !is(typeof({ Unqual!T t = void; immutable T u
= t; }));
static assert(!hasMutableIndirection!(int));
static assert(!hasMutableIndirection!(int[3]));
static assert( hasMutableIndirection!(Object));

@property auto dup(E)(inout(E)[] arr) pure @trusted
    static if (hasMutableIndirection!E)
        auto copy = new E[](arr.length);
        copy[] = cast(E[])arr[];        // assume constant
        return cast(inout(E)[])copy;    // assume constant
        auto copy = new E[](arr.length);
        copy[] = arr[];
        return copy;

In above dup function,
- If E has some mutable indirections, returns inout(E) and has constant purity.
- If E has no mutable indirections, return E[] and has string purity.
  That means: return value is implicitly convertible to immutable(E)[] with
  issue 5081, and can make a immutable duplication from mutable array.

Finally, we can remove the duplication of built-in dup/idup.

Configure issuemail:
------- You are receiving this mail because: -------

Reply via email to