mirror of
https://github.com/dlang/phobos.git
synced 2025-05-05 17:42:58 +03:00
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:
parent
3fec190b7f
commit
cdd2acb0d5
1 changed files with 18 additions and 14 deletions
|
@ -1088,25 +1088,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
|
||||
|
@ -1126,6 +1118,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.
|
||||
|
@ -2301,6 +2299,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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue