(non)nullable types

Jarrett Billingsley jarrett.billingsley at gmail.com
Sat Feb 14 05:55:09 PST 2009


On Sat, Feb 14, 2009 at 8:37 AM, Christopher Wright <dhasenan at gmail.com> wrote:
>> void Foo(void delegate()? dg) //nullable
>> {
>>    dg();
>> }
>
> You're right for that small example. Now let's say you have an object a
> dozen methods referencing the same nullable member. They're private methods
> called by public methods that check if the member is null first. I don't
> want to have to check whether the variable is null every single time I use
> it; I want to do it once at the start and assume (since it's a
> single-threaded application and I'm not assigning to that member) that that
> check works for everything.

....that's the entire point of non-null references.

class Foo
{
    private void delegate() dg; // non-null

    public this(void delegate()? dg) // nullable
    {
        if(dg !is null)
            this.dg = dg; // hey, dg must be non-null!
        else
            throw new Exception("NO! *SLAM*");
    }

    private void method1()
    {
        // use dg here, and you're assured that it's non-null
        // all methods are guaranteed the same thing
    }
}

Why on earth would you rather use a nullable reference when you KNOW
that it should never be null?



More information about the Digitalmars-d mailing list