https://issues.dlang.org/show_bug.cgi?id=17440
Issue ID: 17440
Summary: Nullable.nullify() resets referenced object
Product: D
Version: D2
Hardware: x86_64
OS: Linux
Status: NEW
Severity: enhancement
Priority: P1
Component: phobos
Assignee: [email protected]
Reporter: [email protected]
I am not sure if it is desired, but it was highly unintuitive and unexpected
for me:
If you use Nullable with a class type and call .nullify(), all members of the
class are set to the uninitialized state. Example:
class Test
{
int a;
int[] b;
char[2] c;
this ()
{
a = 5;
b = [1,2,3,4];
c[] = 'B';
}
}
void main ()
{
import std.typecons;
import std.stdio;
import core.memory;
// Just to make sure it doesn't interfere in the test
GC.disable();
Nullable!Test test = new Test;
Test t2 = test;
writefln("before nullify %s %s %s", t2.a, t2.b, t2.c);
test.nullify;
writefln("after nullify %s %s %s", t2.a, t2.b, t2.c);
}
Output is:
before nullify 5 [1, 2, 3, 4] BB
after nullify 0 [] ÿÿ
My expectation would have been that test's reference would simply be set to
null, leaving the referenced memory untouched.
--