Inability to dup/~ for const arrays of class objects
Kenji Hara
k.hara.pg at gmail.com
Thu May 30 19:13:05 PDT 2013
Maybe this is too late replying, but I'd like to provide an information.
Current D does not provide generic way for deep copy of class object.
But, you can use std.conv.to with adding a kind of copy constructor.
class C
{
int x;
this(int n) { x = n; }
// Do deep copy, this is used by to!(array-type)(array)
this(const C c) { this.x = c.x; }
}
void main()
{
const(C)[] carr = [new C(1), new C(2)];
// C[] marr = carr.dup;
// --> Error: cannot implicitly convert element type const(C) to
mutable in carr.dup
import std.conv;
C[] marr = carr.to!(C[]);
// For class arrays which need copy elements,
// std.conv.to returns [new C(carr[0]), new C(carr[1]), ...]
// modify element of returned array
marr[0].x = 5;
// Right now carr[0] and marr[0] are completely unrelated objects
assert(carr[0].x == 1);
assert(marr[0].x == 5);
}
Kenji Hara
2013/5/29 Peter Williams <pwil3058 at bigpond.net.au>
> On 28/05/13 23:41, Steven Schveighoffer wrote:
>
>> On Sat, 25 May 2013 23:58:39 -0400, Peter Williams
>> <pwil3058 at bigpond.net.au> wrote:
>>
>> Is the inability to use dup and ~ with const arrays of class objects a
>>> deliberate design restriction? I couldn't find mention of it in the
>>> specification or Andrei's book and only discovered it the hard way.
>>>
>>
>> It has to be. There is no cdup.
>>
>> For any const(T)[] x, the type of x.dup is T[].
>>
>
> Yes, that's why I was doing the dup. I wanted a non const copy of the
> array.
>
>
> Because this would mean
>> that you would remove const, you cannot do that.
>>
>
> 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.
>
>
> Nor can you idup,
>> since implicit conversion to immutable is not possible.
>>
>> As far as I know, ~ works with const arrays of class objects. Can you
>> give a case where it fails?
>>
>
> 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.
>
> 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.
>
> 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.)
>
> 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.
>
> Thanks for your response,
> Peter
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20130531/e03a6a5a/attachment-0001.html>
More information about the Digitalmars-d
mailing list