The UNION @ statement defines groups of fields that share memory at runtime.
UNION MAP field-declaration field-declaration ... MAP field-declaration field-declaration ... END MAP END UNION
A MAP statement defines alternate groups of fields in a union. During execution, one map at a time is associated with a shared storage location. When you reference a field in a map, the fields in any previous map become undefined, and are succeeded by the fields in the map of the newly referenced field. Also:
A UNION declaration can appear only within a STRUCTURE declaration.
The amount of memory used by a union is that of its biggest map.
Within a UNION declaration, the order of the MAP statements is not relevant.
The UNION line is part of an inherently multiline group of statements, and neither the UNION line nor the END UNION line has any special indication of continuation. You do not put a nonblank in column six, nor an & in column one.
Each field-declaration in a map declaration can be one of the following:
Declaration of a typed data field
STRUCTURE /STUDENT/ CHARACTER*32 NAME INTEGER*2 CLASS UNION MAP CHARACTER*16 MAJOR END MAP MAP INTEGER*2 CREDITS CHARACTER*8 GRAD_DATE END MAP END UNION END STRUCTURE RECORD /STUDENT/ PERSON
In the above example, the variable PERSON has the structure /STUDENT/, so:
PERSON.MAJOR references a field from the first map; PERSON.CREDITS references a field from the second map.
If the variables of the second map field are initialized, and then the program references the variable PERSON.MAJOR, the first map becomes active, and the variables of the second map become undefined.