www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 17440] New: Nullable.nullify() resets referenced object


          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: nobody puremagic.com
          Reporter: dmdtracker supradigital.org

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

    Nullable!Test test = new Test;

    Test t2 = test;

    writefln("before nullify %s %s %s", t2.a, t2.b, t2.c);


    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.

May 26 2017