NullableRef

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).

Constructors

this
this(T* value)

Constructor binding this to value.

Alias This

get

Implicitly converts to T. this must not be in the null state.

Members

Functions

bind
void bind(T* value)

Binds the internal state to value.

nullify
void nullify()

Forces this to the null state.

opAssign
void opAssign(T value)

Assigns value to the internally-held state.

Properties

get
inout(T) get [@property getter]

Gets the value. this must not be in the null state. This function is also called for the implicit conversion to T.

isNull
bool isNull [@property getter]

Returns true if and only if this is in the null state.

Examples

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);

Meta

Suggestion Box / Bug Report