[Issue 8409] New: Proposal: implement arr.dup in library
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Sun Jul 22 05:38:10 PDT 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8409
Summary: Proposal: implement arr.dup in library
Product: D
Version: D2
Platform: All
OS/Version: All
Status: NEW
Severity: enhancement
Priority: P2
Component: druntime
AssignedTo: nobody at puremagic.com
ReportedBy: k.hara.pg at gmail.com
--- Comment #0 from Kenji Hara <k.hara.pg at gmail.com> 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
}
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.
--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
More information about the Digitalmars-d-bugs
mailing list