shallow copy of const(Object)[]

anonymous via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Oct 31 11:38:59 PDT 2014


I have a const(Object)[] and I want a shallow copy of the array.
.dup doesn't do it, which I thought a bug, but according to
Martin Nowak it's by design [1].
std.array.array fails, too. Is there really nothing in phobos for
this?

static import std.array;
void main()
{
      const(Object)[] a;

      version(dup) auto b = a.dup;
      /* Nope. Apparently, dup is supposed to convert the elements
to mutable [1],
      which doesn't work with const(Object), of course. */

      version(array) auto c = std.array.array(a);
      /* Nope. Tries to convert to mutable, too? */

      version(meh)
      {
          typeof(a) d;
          d.reserve(a.length);
          foreach(e; a) d ~= e;
      }
}

[1]
https://github.com/D-Programming-Language/druntime/pull/1001#discussion_r19674927


More information about the Digitalmars-d-learn mailing list