nullable

Defines a value paired with a distinctive "null" state that denotes the absence of a value. If default constructed, a Nullable!T object starts in the null state. Assigning it renders it non-null. Calling nullify can nullify it again.

Practically Nullable!T stores a T and a bool.

  1. auto nullable(T t)
    nullable
    (
    T
    )
    (
    T t
    )
  2. auto nullable(T t)
  3. struct Nullable(T)

Examples

1 struct CustomerRecord
2 {
3     string name;
4     string address;
5     int customerNum;
6 }
7 
8 Nullable!CustomerRecord getByName(string name)
9 {
10     //A bunch of hairy stuff
11 
12     return Nullable!CustomerRecord.init;
13 }
14 
15 auto queryResult = getByName("Doe, John");
16 if (!queryResult.isNull)
17 {
18     //Process Mr. Doe's customer record
19     auto address = queryResult.get.address;
20     auto customerNum = queryResult.get.customerNum;
21 
22     //Do some things with this customer's info
23 }
24 else
25 {
26     //Add the customer to the database
27 }
import std.exception : assertThrown;

auto a = 42.nullable;
assert(!a.isNull);
assert(a.get == 42);

a.nullify();
assert(a.isNull);
assertThrown!Throwable(a.get);

Meta

Suggestion Box / Bug Report