ONC+ Developer's Guide

Unions

XDR unions are discriminated unions, and do not look like C unions - they are more similar to Pascal variant records:

union-definition:

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

case-list:
   "case" value ":" declaration ";"
 	"case" value ":" declaration ";" case-list
 	"default" ":" declaration ";" 

The following is an example of a type returned as the result of a "read data" operation: If there is no error, return a block of data; otherwise, don't return anything.

union read_result switch (int errno) {
 	case 0:
      opaque data[1024];
 	default:
 		void;
 	};

It compiles into the following:

struct read_result {
 	int errno;
 	union {
      char data[1024];
 	} read_result_u;
};
typedef struct read_result read_result;

Notice that the union component of the output struct has the same name as the type name, except for the trailing _u.