Data Type ‘ANY’ and ‘ANY_<type>’

When implementing a function, if one of its function inputs (VAR_INPUT) has a generic ICE data type (ANY or ANY_<type>), then the data type of the call parameter is not defined as unique. Variables of different data types can be passed to this function. The passed value and its type can be queried within the function via a predefined structure.

With compiler version 3.5.1.0 and later, the generic IEC data types listed below are supported when using function inputs. The table lists the generic data type that permits elementary data types.

Hierarchy of Generic Data Types Elementary Data Types
ANY ANY_BIT   BOOL, BYTE, WORD, DWORD, LWORD
ANY_DATE   DATE_AND_TIME, LDT, DATE, TIME_OF_DAY, LTOD
ANY_NUM ANY_REAL REAL, LREAL
ANY_INT

USINT, UINT, UDINT, ULINT

SINT, INT, DINT, LINT

ANY_STRING   STRING, WSTRING

Example

FUNCTION ANYBIT_TO_BCD : BCD
   VAR_INPUT
      value : ANY_BIT;
   END_VAR

If the function ANYBIT_TO_BCD is called, then a variable of data type BOOL, BYTE, WORD, DWORD, or LWORD can be passed to the function as a parameter.

Predefined structure

When compiling the code, an ANY data type is replaced internally with the following structure:

TYPE AnyType :
STRUCT
        // the type of the actual parameter
\ typeclass : __SYSTEM.TYPE_CLASS ;
        // the pointer to the actual parameter
\ pvalue : POINTER TO BYTE;
        // the size of the data, to which the pointer points
\ diSize : DINT;
END_STRUCT
END_TYPE

The actual call parameter will assign the structure elements at runtime.

Example

This compares whether or not the two passed variables have the same type and the same value.

FUNCTION Generic_Compare : BOOL
VAR_INPUT
\ any1 : ANY;
\ any2 : ANY;
END_VAR
VAR
\ pTest : POINTER TO ARRAY [0..100] OF POINTER TO DWORD;
\ icount: DINT;
END_VAR

pTest := ADR(any1);
Generic_Compare := FALSE;
IF any1.typeclass <> any2.typeclass THEN
\ RETURN;
END_IF
IF any1.diSize <> any2.diSize THEN
\ RETURN;
END_IF
// Byte comparison
FOR icount := 0 TO any1.diSize-1 DO
\ IF any1.pvalue[iCount] <> any2.pvalue[iCount] THEN
\ \ RETURN;
\ END_IF
END_FOR
Generic_Compare := TRUE;
RETURN;
// END_FUNCTION