The new ?? and ??? operators

Janice Caron caron800 at googlemail.com
Tue Sep 25 04:09:50 PDT 2007


On 9/25/07, Rioshin an'Harthen <rharth75 at hotmail.com> wrote:
> GetLong, as the name suggests, is used in this example to return a long
> (signed 64-bit value) from the database column ID, with the current database
> row referenced through the variable data (which, e.g. could be of type
> MySqlDataReader). GetLong may, if the column (in the row accessed) doesn't
> contain a value, return null, to signify this, since 0 (or any other value)
> may be a legal value in the database.

You contradict yourself. Clearly it does /not/ return a ulong, since a
ulong cannot contain null (or null is indistinguishable from zero,
depending on your point of view).

So what you really mean is that the type ulong? (with a question mark)
is basically equivalent to

struct
{
    ulong n;
    bool isNull;
}

which implicitly casts to its n member, and that ?? tests the isNull field.

I'm sure this could be done with templates. That is:

    Nullable!(long) id = data.GetNullableLong("ID");

    if (isNulled(id)) { /* ... */ }

    long n = id; // implicit cast

Or, all in one step, your

   long id = data.GetLong("ID") ?? 0;

would become

    long id = isNulled(auto temp = data.GetLong("ID")) ? 0 : temp;



More information about the Digitalmars-d mailing list