Skip to content

Commit

Permalink
Merge pull request #4923 from ntrel/variant-opIndexOpAssign
Browse files Browse the repository at this point in the history
Fix Issue 16758 - Variant.opIndex result not modified after opAssign
  • Loading branch information
JackStouffer committed Dec 6, 2016
2 parents eef757a + cdd2acb commit 94d3786
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions std/variant.d
Original file line number Diff line number Diff line change
Expand Up @@ -1090,25 +1090,17 @@ public:
///
unittest
{
auto a = Variant(new int[10]);
Variant a = new int[10];
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 ];
a = hash;
assert(a[42] == 24);
}

/** 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
a[42] /= 2;
assert(a[42] == 12);
}

unittest
Expand All @@ -1135,6 +1127,12 @@ public:
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,
* returns the _length of that array. Otherwise, throws an
* exception.
Expand Down Expand Up @@ -2341,6 +2339,12 @@ unittest
assert(b == a);
}

unittest
{
const Variant a = [2];
assert(a[0] == 2);
}

unittest
{
// http://d.puremagic.com/issues/show_bug.cgi?id=10017
Expand Down

0 comments on commit 94d3786

Please sign in to comment.