How to use nullable types

  • version

    1.1.1

  • scope

    Example.

    This code is provided as example code for a user to base their code on.

  • description

    How to use nullable types

  • boards

    Unless otherwise specified, this example runs on the SliceKIT Core Board, but can easily be run on any XMOS device by using a different XN file.

Resource types (e.g. ports, timers etc) and reference types can be made nullable. This means that their value could be a normal value or could be the special value null. The ? type operator creates a nullable type.

For example the following function has a nullable port argument:

void f(port ?p)
{
  if (!isnull(p)) {
    printf("Outputting to port\n");
    p <: 0;
  }
}

The isnull function tests whether a variable of nullable type is null or not.

Functions taking nullable arguments can either be passed a value or the null value:

void g() {
  f(null);
  f(p);
}