1 import std.exception : assertThrown; 2 3 int x = 5, y = 7; 4 auto a = nullableRef(&x); 5 assert(!a.isNull); 6 assert(a == 5); 7 assert(x == 5); 8 a = 42; 9 assert(x == 42); 10 assert(!a.isNull); 11 assert(a == 42); 12 a.nullify(); 13 assert(x == 42); 14 assert(a.isNull); 15 assertThrown!Throwable(a.get); 16 assertThrown!Throwable(a = 71); 17 a.bind(&y); 18 assert(a == 7); 19 y = 135; 20 assert(a == 135);
Just like Nullable!T, except that the object refers to a value sitting elsewhere in memory. This makes assignments overwrite the initially assigned value. Internally NullableRef!T only stores a pointer to T (i.e., Nullable!T.sizeof == (T*).sizeof).