Titan



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. 

Related keywords:


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


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