View source code Display the source code in std/typecons.d from which this page was generated on github. Improve this page Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone. Page wiki View or edit the community-maintained wiki page associated with this page.

std.typecons.nullable - multiple declarations

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

Constructors

Name Description
this Constructor initializing this with value.

Properties

Name Type Description
get [get] inout(T) Gets the value. this must not be in the null state. This function is also called for the implicit conversion to T.
isNull [get] bool Check if this is in the null state.

Templates

Name Description
nullify Forces this to the null state.
opAssign Assigns value to the internally-held state. If the assignment succeeds, this becomes non-null.

Example

struct CustomerRecord
{
    string name;
    string address;
    int customerNum;
}

Nullable!CustomerRecord getByName(string name)
{
    //A bunch of hairy stuff

    return Nullable!CustomerRecord.init;
}

auto queryResult = getByName("Doe, John");
if (!queryResult.isNull)
{
    //Process Mr. Doe's customer record
    auto address = queryResult.address;
    auto customerNum = queryResult.customerNum;

    //Do some things with this customer's info
}
else
{
    //Add the customer to the database
}

Struct Nullable

Just like Nullable!T, except that the null state is defined as a particular value. For example, Nullable!(uint, uint.max) is an uint that sets aside the value uint.max to denote a null state. Nullable!(T, nullValue) is more storage-efficient than Nullable!T because it does not need to store an extra bool.

Constructors

Name Description
this Constructor initializing this with value.

Properties

Name Type Description
get [get] inout(T) Gets the value. this must not be in the null state. This function is also called for the implicit conversion to T.
isNull [get] bool Check if this is in the null state.

Templates

Name Description
nullify Forces this to the null state.
opAssign Assigns value to the internally-held state. If the assignment succeeds, this becomes non-null. No null checks are made. Note that the assignment may leave this in the null state.

Parameters

NameDescription
T The wrapped type for which Nullable provides a null value.
nullValue The null value which denotes the null state of this Nullable. Must be of type T.

Example

Nullable!(size_t, size_t.max) indexOf(string[] haystack, string needle)
{
    //Find the needle, returning -1 if not found

    return Nullable!(size_t, size_t.max).init;
}

void sendLunchInvite(string name)
{
}

//It's safer than C...
auto coworkers = ["Jane", "Jim", "Marry", "Fred"];
auto pos = indexOf(coworkers, "Bob");
if (!pos.isNull)
{
    //Send Bob an invitation to lunch
    sendLunchInvite(coworkers[pos]);
}
else
{
    //Bob not found; report the error
}

//And there's no overhead
static assert(Nullable!(size_t, size_t.max).sizeof == size_t.sizeof);

Authors

Andrei Alexandrescu, Bartosz Milewski, Don Clugston, Shin Fujishiro, Kenji Hara

License

Boost License 1.0.

Comments