<div dir="ltr"><div>Maybe this is too late replying, but I'd like to provide an information.</div><div><br></div><div>Current D does not provide generic way for deep copy of class object.</div><div>But, you can use <a href="http://std.conv.to">std.conv.to</a> with adding a kind of copy constructor.</div>
<div><br></div><div>class C</div><div>{</div><div>    int x;</div><div>    this(int n) { x = n; }</div><div><br></div><div>    // Do deep copy, this is used by to!(array-type)(array)</div><div>    this(const C c) { this.x = c.x; }</div>
<div>}</div><div>void main()</div><div>{</div><div>    const(C)[] carr = [new C(1), new C(2)];</div><div>    // C[] marr = carr.dup;</div><div>    // --> Error: cannot implicitly convert element type const(C) to mutable in carr.dup</div>
<div><br></div><div>    import std.conv;</div><div>    C[] marr = <a href="http://carr.to">carr.to</a>!(C[]);</div><div>    // For class arrays which need copy elements,</div><div>    // <a href="http://std.conv.to">std.conv.to</a> returns [new C(carr[0]), new C(carr[1]), ...]</div>
<div><br></div><div>    // modify element of returned array</div><div>    marr[0].x = 5;</div><div><br></div><div>    // Right now carr[0] and marr[0] are completely unrelated objects</div><div>    assert(carr[0].x == 1);</div>
<div>    assert(marr[0].x == 5);</div><div>}</div><div><br></div><div class="gmail_extra">Kenji Hara<br><br><div class="gmail_quote">2013/5/29 Peter Williams <span dir="ltr"><<a href="mailto:pwil3058@bigpond.net.au" target="_blank">pwil3058@bigpond.net.au</a>></span><br>
<blockquote class="gmail_quote" style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div class="im">
On 28/05/13 23:41, Steven Schveighoffer wrote:<br>
<blockquote class="gmail_quote" style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
On Sat, 25 May 2013 23:58:39 -0400, Peter Williams<br>
<<a href="mailto:pwil3058@bigpond.net.au" target="_blank">pwil3058@bigpond.net.au</a>> wrote:<br>
<br>
<blockquote class="gmail_quote" style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
Is the inability to use dup and ~ with const arrays of class objects a<br>
deliberate design restriction?  I couldn't find mention of it in the<br>
specification or Andrei's book and only discovered it the hard way.<br>
</blockquote>
<br>
It has to be.  There is no cdup.<br>
<br>
For any const(T)[] x, the type of x.dup is T[].<br>
</blockquote>
<br></div>
Yes, that's why I was doing the dup.  I wanted a non const copy of the array.<div class="im"><br>
<br>
<blockquote class="gmail_quote" style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
 Because this would mean<br>
that you would remove const, you cannot do that.<br>
</blockquote>
<br></div>
I find that dup works for const T[] when T is not a class (although I haven't tried it with pointers).  This means I write two versions of my code - one for classes (which does (cast(T[])).dup) and one for the rest.<div class="im">
<br>
<br>
<blockquote class="gmail_quote" style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
 Nor can you idup,<br>
since implicit conversion to immutable is not possible.<br>
<br>
As far as I know, ~ works with const arrays of class objects.  Can you<br>
give a case where it fails?<br>
</blockquote>
<br></div>
Looking at my code that caused me to ask this question, I've realised that I'm appending a const object onto a no const array.  Once again this works for non class objects but not for classes.<br>
<br>
I can see why this might be the case as non class objects are probably copied by value where the class objects are pointers and the constness applies to the items in an array as well the array itself.<br>
<br>
If this behaviour is a deliberate design decision I'll accept that and (probably) modify my code to use (cast(T[])).dup in all cases.  (At the moment, I'm using a single mixin template to handle this issue and the inability to use ==, !=, < and friends with constant class objects. When that problem goes away I'll do the above modifications.)<br>

<br>
This is the type of issue that can come as a surprise when you have a working/tested code that suddenly stops compiling when you use it with classes.  So a heads up in the documentation would be useful.<br>
<br>
Thanks for your response,<br>
Peter<br>
</blockquote></div><br></div></div>