On 2010-05-22 07:56:31 -0400, Michel Fortin <[email protected]> said:
@explicitdup struct Array { }
void testVal(Array array);
void testRef(ref Array array);
unittest {
Array array;
testVal(array); // error, cannot copy array implicitly
testVal(array.dup); // ok, array is copied
testRef(array); // ok, array is passed by reference
}
If there's already a way to achieve this, I couldn't find it.
Apparently it's already achievable this way:
struct Array {
@disable this(this);
Array dup() { return Array(...); }
...
}
It also blocks simple assignments:
Array array2 = array; // error, array is not copyable
Array array3 = array.dup; // ok
With this, I don't think we need containers to be reference types
anymore. The naive error of copying containers everywhere without you
knowing about it (as it occurs in C++) is simply no longer possible.
--
Michel Fortin
[email protected]
http://michelf.com/