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.
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.