Unions

XDR unions are discriminated unions and look different from C unions. XDR unions are more analogous to Pascal variant records than to C unions.

Following is an XDR union definition:

union-definition:
      "union" union-ident "switch" "(" declaration ")" "{"
           case-list
      "}"

case-list:
      "case" value ":" declaration ";"
      "default" ":" declaration ";"
      "case" value ":" declaration ";" case-list
Following is an example of a type that might be returned as the result of a read data operation. If there is no error, the type returns a block of data; otherwise, it returns nothing.

union read_result switch (int errno) {
case 0
     opaque data[1024];
default:
     void;
};
The type is compiled into the following structure:

struct read_result {
     int errno;
     union {
          char data[1024];
     }read_result_u;
};
typedef struct read_result read_result;
Note: The union component of this output structure is identical to the type, except for the trailing _u.