References in D

Andrej Mitrovic andrej.mitrovich at gmail.com
Mon Sep 24 13:48:05 PDT 2012


On 9/24/12, bearophile <bearophileHUGS at lycos.com> wrote:
> Try to start using Nullable of Phobos:
> http://dlang.org/phobos/std_typecons.html#Nullable

It could be a little more usable if it was (pseudocode untested):

struct Nullable(P)
{
    P payload;
    bool _isNull = true;

    void opAssign(P p)
    {
        payload = p;
        _isNull = false;
    }

    bool opEquals(typeof(null) t)
    {
        return _isNull;
    }

    bool opEquals(P p)
    {
        return !_isNull && payload == p;
    }
}

void main()
{
    Nullable!int a;
    assert(a == null);
    a = 5;
    assert(a != null);
    assert(a == 5);
}

Unfortunately (or fortunately) you can't implement an 'is' operator overload.


More information about the Digitalmars-d mailing list