Skip to content

type check expression

Cameron Purdy edited this page Apr 8, 2020 · 3 revisions

The .is() and .as() forms of the PostfixExpression are used to determine if a reference is of a particular type, or to assume (assert) that a reference is of a particular type.

a.is(b)
a.as(b)

The implicit type of the IsExpression is Boolean. The expression evaluates to True if the expression a is of the specified type b, and False otherwise.

The implicit type of the AsExpression is the specified type. The expression evaluates to a value of the specified type b if the expression a is of that specified type, and throws a TypeMismatch otherwise. (The AsExpression acts as a type assertion.)

Here is a simple code example that shows both of these expression forms:

// if the value is immutable, then assume that the
// value is a String, and print the String value
if (value.is(immutable Object))
    {
    printName(value.as(String));
    }

While both of these expression forms appear to be method or function invocations, they are not. Furthermore, these are not operators in the same sense as addition or multiplication, which are implemented as methods on the classes of the operands. Instead, these are keywords (is and as) designed to appear as method invocations, and are implemented by specific XVM instructions, including JMP_TYPE and CAST.

The expression short-circuits if a short-circuits.

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.
    IsExpression:
        PostfixExpression .is ( TypeExpression )

    AsExpression:
        PostfixExpression .as ( TypeExpression )