Hi, guys. — said the shy newcomer.
import myproject.helpers.UniqueArray;
void main(){auto a0 = [1, 2, 3];
// I'm not yet sure how to go about the constructor, since:
auto a1 = UniqueArray!(int)(a0[1 .. $]); // error: should not be able to internally hold reference to// a raw array since this could be used to break the "unique// elements" contract promise of UniqueArray// copy of elements can be considered, but I'd rather// have clients copy the array themselves so that they// know it is happening
auto a2 = UniqueArray!(int)(a0[1 .. $].dup); // should be fine if D had some sort of non-const// rvalue reference support, but I think it does not;// am I wrong?auto a3 = UniqueArray!(int)(a0[1 .. $].idup); // semantically pleasing at first sight, but// suboptimal: the constructor would have to copy// the passed array again to get rid of immutabilityauto a4 = bestOptionOutOf(a1, a2, a3); // (:a4[1 .. $] = [3, 4, 5]; // ok: would first construct a UniqueArray out of the rvalue (thus ensuring// "uniqueness" of elements) and then would work like a usual slice// assignmenta4 ~= 5; // throws exception: 5 is already in the array!a4 ~= 6; // ok: 6 is not therewriteln(a4); // ok, output: [2, 3, 4, 5, 6]// could just implement UniqueArray.toString() for this to work, but making UniqueArray// properly model the ranges an array models solves this problem and others at the same// time
auto a5 = a4.dup; // all properties of an array, such as dup here, should hold and overall// the object should behave as one would expect from an array
int[] a6 = a5; // error: obviously shouldn't work since a6 could then be used to break the// UniqueArray contract
}