Optional Data

Optional data is a type of union that occurs so frequently it has its own syntax.

The optional data type is closely coordinated to the representation of recursive data structures by the use of pointers in high-level languages, such as C or Pascal. The syntax for pointers is the same as that for C language.

The syntax for optional data is as follows:

type-name *identifier;
The declaration for optional data is equivalent to the following union:

union switch (bool opted) {
    case TRUE:
    type-name element;
    case FALSE:
    void;
} identifier;
Because bool opted can be interpreted as the length of the array, the declaration for optional data is also equivalent to the following variable-length array declaration:

type-name identifier<1>;
Optional data is very useful for describing recursive data structures such as linked lists and trees. For example, the following defines a stringlist type that encodes lists of arbitrary length strings:

struct *stringlist {
    string item<>;
    stringlist next;
};
The example can be equivalently declared as a union, as follows:

union stringlist switch (bool opted) {
    case TRUE:
        struct {
            string item<>;
            stringlist next;
        } element;
    case FALSE:
        void;
};
The example can also be declared as a variable-length array, as follows:

struct stringlist<1> {
    string item<>;
    stringlist next;
};

Because both the union and the array declarations obscure the intention of the stringlist type, the optional data declaration is preferred.