Skip to content

not null expression

Cameron Purdy edited this page Apr 8, 2020 · 1 revision

The NotNullExpression uses the unary postfix operator ? following an expression:

a?

This expression is a short-circuiting expression.

The execution of the expression first evaluates the expression a. If the result is Null, then this expression will arc. Otherwise, the result of this expression is the result of the expression a.

The short-circuiting of the NotNullExpression is grounded by the ElseExpression. Any number of NotNullExpression occurrences within an expression can be grounded by a single ElseExpression; for example:

Int size = company?.department?.people?.size : 0;

That one line of code is roughly equivalent to the following Gustave Flaubert novel:

Int size;
Company company = company;
if (company != Null)
    Department department = company.department;
    if (department != Null)
        {
        List<Employee> people = deparment.people;
        if (people != Null)
            {
            size = people.size;
            }
        else
            {
            size = 0;
            }
        }
    else
        {
        size = 0;
        }
    }
else
    {
    size = 0;
    }

The type of the expression is the type of the expression a with the Nullable type removed. The type of the expression a must indicate that the expression could be Null; otherwise, a compile-time error occurs.

The expression short-circuits if a short-circuits, or if the expression a yields a Null value.

The expression uses the default left-to-right definite assignment rules:

  • The VAS before a is the VAS before the expression.
  • The VAS after the expression is the VAS after a.

The NoWhitespace requirement is used to differentiate this expression from the TernaryExpression:

    NotNullExpression:
        PostfixExpression NoWhitespace ?