Fix Issue 16758 - Variant.opIndex result not modified after opAssign

Add VariantN.opIndexOpAssign.
Also undocument & tweak test for const(Variant).opIndex.
This commit is contained in:
Nick Treleaven 2016-11-23 11:30:39 +00:00
parent 3fec190b7f
commit cdd2acb0d5

View file

@ -1088,25 +1088,17 @@ public:
/// ///
unittest unittest
{ {
auto a = Variant(new int[10]); Variant a = new int[10];
a[5] = 42; a[5] = 42;
assert(a[5] == 42); assert(a[5] == 42);
assert((cast(const Variant) a)[5] == 42); a[5] += 8;
assert(a[5] == 50);
int[int] hash = [ 42:24 ]; int[int] hash = [ 42:24 ];
a = hash; a = hash;
assert(a[42] == 24); assert(a[42] == 24);
} a[42] /= 2;
assert(a[42] == 12);
/** Caveat:
Due to limitations in current language, read-modify-write
operations $(D op=) will not work properly:
*/
unittest
{
Variant a = new int[10];
a[5] = 42;
a[5] += 8;
//assert(a[5] == 50); // will fail, a[5] is still 42
} }
unittest unittest
@ -1126,6 +1118,12 @@ public:
return args[0]; return args[0];
} }
/// ditto
Variant opIndexOpAssign(string op, T, N)(T value, N i)
{
return opIndexAssign(mixin(`opIndex(i)` ~ op ~ `value`), i);
}
/** If the $(D VariantN) contains an (associative) array, /** If the $(D VariantN) contains an (associative) array,
* returns the length of that array. Otherwise, throws an * returns the length of that array. Otherwise, throws an
* exception. * exception.
@ -2301,6 +2299,12 @@ unittest
assert(b == a); assert(b == a);
} }
unittest
{
const Variant a = [2];
assert(a[0] == 2);
}
unittest unittest
{ {
// http://d.puremagic.com/issues/show_bug.cgi?id=10017 // http://d.puremagic.com/issues/show_bug.cgi?id=10017