Skip to content

Commit

Permalink
Don't call destroy on reference types when setting to none
Browse files Browse the repository at this point in the history
Fixes #20
  • Loading branch information
aliak00 committed Aug 24, 2018
1 parent 560e6a2 commit 0c96802
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
6 changes: 5 additions & 1 deletion source/optional/optional.d
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,11 @@ struct Optional(T) {
*/
void opAssign()(const None) if (isMutable!T) {
if (!this.empty) {
destroy(this._value);
static if (isNullInvalid) {
this._value = null;
} else {
destroy(this._value);
}
this._empty = true;
}
}
Expand Down
15 changes: 15 additions & 0 deletions tests/optional.d
Original file line number Diff line number Diff line change
Expand Up @@ -453,3 +453,18 @@ unittest {
n = john.dispatch.residence.dispatch.numberOfRooms;
assert(n == some(1));
}

@("Should not destroy references")
unittest {
class C {
int i;
this(int ai) { i = ai; }
}

C my = new C(3);
Optional!C opt = some(my);
assert(my.i == 3);

opt = none;
assert(my.i == 3);
}

0 comments on commit 0c96802

Please sign in to comment.