4.4 Using the anyvalue Data Type

If the type anyvalue is defined as a parameter of a COBOL function in the IDM dialog script, then any data type is permitted for this parameter. In the COBOL program, such a parameter is passed to the program as a pointer. To use a value of this kind in interface functions that expect a DM-Value structure, the parameter can be assigned to DM-value-pointer and DM-datatype can be set to DT-anyvalue. This can be understood in a way that the DM-Value structure may contain any data type and that the actual value is stored as a pointer.

Regardless of how an input parameter had been defined, a return value or output parameter whose data type can be stored directly in the DM-Value structure will actually be stored directly. For instance, DT-anyvalue would become DT-string.

Collection data types cannot be stored directly, so they are always returned as DT-anyvalue. Such a DT-anyvalue value may be processed with the DMcob_Value* functions.

Please Note

The data type anyvalue indicates in the IDM dialog script that any data type is allowed. At runtime, however, an attribute, a variable, a parameter, or a return value always has a value with a specific data type that does not equal anyvalue.

Example

The following definition of a rule indicates that the return value may be of any data type:

rule anyvalue RuleAny(integer I) {
  case I
    in 1: return "Hello";
    in 2: return 42;
    otherwise:
      return void;
  endcase
}

However, at runtime, not anyvalue will be returned, but always the type of the return statement, for example print typeof(RuleAny(1)); will output the value string.

An exception to the rules above is a Managed Value created with DMcob_ValueInit. A Managed Value is always preserved. If a Managed Value is used as DM-value-pointer in a DM-Value structure (DM-datatype must be set to DT-anyvalue), then the data type DT-anyvalue is retained. This also applies to return values and output parameters.

Life Time of DM-value-pointer Values

A Managed Value that has not been created globally is only valid until the end of the function in which it was created. It can still be returned to the IDM however.

The same applies to pointer values passed as anyvalue parameters to a function. They only remain valid until the end of the function as well. Returning them to the IDM is allowed.

All other pointer values, which for example were created to return a collection data type, are only valid until the next time a DMcob* function is invoked. They also must not be returned to the IDM. To enable these values to be processed, the DMcob_Value* functions do not destroy their validity.

It is recommended to use Managed Values for collection data types right from the start.