D-Programming-Language/druntime — pull request #763 by monarchdodra (2014-04-14 10:21:22+00:00)
Title: Issue 12512: Allow dup on non-castable-mutable types

This address issue 12512, where you can't call dup on const types that can't be implicitly cast to mutable.

It changes the behavior of dup (technically, it "adds" new behavior) to provide "as mutable a result as possible". EG: it returns `const(T)`.

This new behavior may not sound like much, but it is important for generic code, which wouldn't be able to use `dup`/`idup` otherwise. With this new feature, I am able to add this to `array`:

```D
static if (isDynamicArray!Range)
{
static if (is(E == immutable))
return r.idup;
else
return r.dup;
}
else static if ( ...
```
This allows `array` to support any slice type, while leveraging dup's superior performance:

```D
unittest
{
static struct S{int* p;}
foreach ( SS; TypeTuple!(S, const(S), immutable(S)))
{
SS[] a = array(SS.init.repeat(5));
a = array(a);
}
}
```
--------
Assigning to @WalterBright for review of functionality. And @MartinNowak for code review.