# union

The keyword denotes a structured type: a collection of fields, each one identified by an identifier. Only one of the specified fields will ever be present in an actual union value.

- Union types are useful to model a structure which can take one of a finite number of known types.
- Optional elements are forbidden (they do not make sense).
- Element type can be any basic or user-defined data type.
- More elements can have same type as long as their identifiers differ.
- Elements of the union may be accessed using the dot notation or assignment notation.
- The ischosen() predefined function returns the value true if and only if the referenced element of the union type is actually selected for the given union.

Related keywords:

- type
- address
- ischosen

### type union (identifier | address) { type_reference element_identifier ... };

- The type union keywords introduce the type definition.

- identifier is the name used to refer to the union. Must begin with a letter, may contain letters, numbers and underscore characters.

- address is a user defined type to allow addressing specific entities inside the System Under Test.

- type_reference refers to an already defined (structured or simple) type.

- element_identifier identifies the elements of the union. The identifiers must be unique within the same union

- ... indicates that type_reference element_identifier pairs may be repeated. They are separated by comma.

Example 1: type definition

type union MyUnionType {
integer Number1,
integer Number2,
charstring String
}

The union called MyUnionType consists of three elements. The first two are both of type integer and have the identifier Number1 and Number2, respectively. The third element is of type character string and has the identifier String.

Example 2: dot notation (For the type definition see example 1)

var MyUnionType v_myUnion;
v_myUnion.Number1 := 12;

The variable v_myUnion of type MyUnionType is defined. The value 12 is assigned to the field Number1 making the given field to be the chosen one

Example 3: assignment notation (For the type and variable definition see example 1 & 2)

v_myUnion := {Number2 := 112};

The value 112 is assigned to the field Number2.

Example 4: the predefined function ischosen

var boolean v_whichone := ischosen(v_myUnion.Number1);

The variable v_whichone will have the value true if the field Number1 is chosen (as in example 2).

BNF definition of union