Bug 8409 – Proposal: implement arr.dup in library

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
druntime
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-07-22T05:38:00Z
Last change time
2014-05-28T11:23:56Z
Assigned to
nobody
Creator
k.hara.pg
Depends on
6930, 8408

Comments

Comment #0 by k.hara.pg — 2012-07-22T05:38:09Z
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 } else { 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.
Comment #1 by k.hara.pg — 2012-09-09T06:21:35Z
Comment #2 by k.hara.pg — 2013-12-09T17:27:19Z
A new definition of library dup function, by using issue 6930. inout(E)[] dup(E)(const(inout(E))[] arr) pure; 'inout const E' is not implicitly convertible to 'inout E', so the returned value is isolated from the given argument.
Comment #3 by k.hara.pg — 2014-05-28T11:23:56Z