What is special about an immutable array of class objects, and why can I not .dup ?

James Blachly james.blachly at gmail.com
Mon May 28 13:51:49 UTC 2018

Consider the below:

class C
     int x;

struct S
     int x;

void main()
	immutable C[] c = [ new C(), new C()];
	immutable S[] s = [ S(), S() ];
     immutable int[] i = [ 1, 2 ];

     auto x = c.dup;
     auto y = s.dup;
     auto z = i.dup;


This fails to compile with a `.dup` template matching error at 
line `auto x = c.dup;`. However, calling `.idup` works just fine. 
The immutable struct array and int array of course `.dup` just 

I would have guessed that changing the definition of `C[]` to 
`immutable(C)[]` would have also helped, but it did not.

Why are the class objects special in this case, and why does 
`immutable(C)[]` not help?   I believed that this defined a 
dynamic array `c` which was itself mutable, the elements of which 
were immutable.

Thanks for insights.

More information about the Digitalmars-d-learn mailing list