Ansicht
Dokumentation

ABENENUMERATED_TYPES_USAGE - ENUMERATED TYPES USAGE

ABENENUMERATED_TYPES_USAGE - ENUMERATED TYPES USAGE

SUBST_MERGE_LIST - merge external lists to one complete list with #if... logic for R3up   Addresses (Business Address Services)  
This documentation is copyright by SAP AG.
SAP E-Book

Enumerated Objects ()

This section summarizes the use of enumerated objects, that is, data objects with enumerated types.

Enumerated Objects, Use

Declaration

An enumerated object is a data object with an enumerated type defined by one of the following TYPES statements:

TYPES BEGIN OF ENUM enum_type ...
  TYPES val1 ...
  TYPES val2 ...
  ...
TYPES END OF ENUM enum_type ...

The technical data type of the content of an enumerated object is the base type of the enumerated type. The possible content is defined by the enumerated values defined using TYPES val1, TYPES val2, ..., of which exactly one value must have the type-specific initial value. The base type is i by default, but it can also be a different elementary data type. The following enumerated objects exist:

  • Enumerated variables
An enumerated variable is a variable defined using
DATA enum_var TYPE enum_type ...
that can contain only enumerated values of the enumerated type. This is ensured by the ABAP runtime framework and the rules for using enumerated types.
  • Enumerated constants
An enumerated constant is used to define a value in the value set of an enumerated type. It is defined in the definition of an enumerated type with
TYPES val ...
under the name val. It is a constant of the context of its definition and contains the enumerated value assigned in the definition. It can be specified in all read positions in which enumerated objects are possible. Its enumerated value is either determined automatically as an integer or can be specified explicitly in the definition.
  • Components of enumerated structures
A component of an enumerated structure is a special form of an enumerated constant that exists as a component of a constant structure and not as a single data object. The enumerated structure struc is defined using
TYPES BEGIN OF ENUM enum_type STRUCTURE struc ...
This makes the enumerated constants defined using
TYPES val ...
into their structure components. Otherwise, the same applies as to regular enumerated constants.

Notes

  • Enumerated objects are mainly used to check allowed values. This usually restricts the actual parameters passed to methods to the enumerated values defined in the class.
  • The base type and the actual enumerated value are almost always ignored when enumerated objects are used. Assignments and comparisons are usually only made between enumerated objects with the same enumerated type.
  • Base types other than i and the actual enumerated values may be significant in transformations between enumerated type or in migrations of previous concepts.
  • Enumerated structures can be used to avoid naming conflicts if there are multiple enumerated types in a namespace. Enumerated structures can be used to enable the use of the same enumerated constant name in multiple enumerated types.

Example

Declaration of an enumerated type size in a class. The method parameter size has the enumerated type and only enumerated objects of this type can be passed to this parameter. This example show how the enumerated constant demo=>l is passed. This guarantees that only enumerated values of the enumerated type can be passed to the parameter. These can be evaluated in comparisons with the enumerated constants. In the CASE control structure shown here, the statement block after WHEN OTHERS can be reached only when demo=>xl and demo=>xxl are passed.

Processing of Enumerated Objects

The processing of enumerated objects is usually independent of the base type of the enumerated type. Only the enumerated type itself is relevant for all rules specified here. Assignments of enumerated objects with a numeric base type, for example, cannot be assigned to numeric target fields and cannot be compared with numeric fields. The enumerated value in the base type can be accessed using the constructor operators CONV and EXACT only.

Operand Positions for Enumerated Objects

  • Read positions
Enumerated objects can be used in all read positions in which the operand type is their enumerated type or in which the operand is converted to one of the character-like types c or string. Substring access is not possible.
  • Write positions
Enumerated variables can only be used in write positions in which the operand type is the enumerated type and only the associated enumerated values can be written. If known statically, an attempt to assign a value other than a valid enumerated value to an enumerated variable produces a syntax error. If not known statically, an exception is raised.

In the first assignment, the enumerated constant xl in a read position is assigned to the enumerated variable size in a write position. The string expression in the second half exploits the fact that the enumerated constants are converted implicitly to the type string before the concatenation. The result is SMLXLXXL.

Value Assignments

Only enumerated objects with the same enumerated type can be assigned to an enumerated variable. In the assignment, the target field is given the enumerated value of the source field.

The initial value of the base type is always a valid enumerated value of an enumerated type. Accordingly, an enumerated variable can be set to the initial value of its base type using CLEAR. Assignments of VALUE enum_type( ) are also possible.

Conversely, enumerated objects can only be assigned to compatible enumerated variables, with the following exception: There is a conversion rule for assignments of enumerated objects to character-like variables of the types c and string. In this case, the target field is assigned the name of the enumerated constant or the component of the enumerated structure under which the enumerated value of the source field is defined in the enumerated type. Since this rule can produce unexpected results, conversions of this type lead to a message from the extended program check that can be hidden using the pragma ##types.

In structures, each component with an enumerated type forms a separate fragment of the structure fragment view. In assignments between structures of this type, the fragment views must match so that only components with the same enumerated type can be assigned to each other.

In the first assignment, the enumerated constant sz-xl is assigned to the enumerated variable size of its enumerated type. This variable then contains the associated enumerated value 3. In the second assignment, the enumerated constant is assigned to the text string size_string. This string is given the value XL in accordance with the conversion rule.

Comparisons

For comparisons between enumerated objects, the comparison rule applies that an enumerated object can only be compared with an enumerated object with the same enumerated type. Here, the values of the operands are compared in accordance with their base type.

Each enumerated type has an initial enumerated value, which makes checks with the predicate expression IS INITIAL possible.

Note

Comparisons do not perform an implicit conversion of an enumerated object to the character-like types c and string. Conversions of this type must be performed explicitly, for example with the conversion operator CONV. This difference between assignments and comparisons is particularly significant when reading from internal tables using the statement READ TABLE or table expressions and the statement LOOP AT or a FOR statement. When individual lines are read, the comparison fields of conditions are converted implicitly; this is not the case when multiple lines are read. Implicit conversions produce the message from the extended program check mentioned above.

The first comparison shows a typical case where an enumerated variable is compared with an enumerated constant. The syntax of the second comparison that is in a comment is not possible. In the third comparison, the enumerated variable is converted explicitly to the type string before the comparison with a text string.

Typing of Formal Parameters and Field Symbols

If formal parameters of procedures or field symbols are typed with an enumerated type, only enumerated objects with the same enumerated type can be assigned to them. As usual, an exception to this are return values of functional methods that can also be converted to character-like objects of the types c and string.

Enumerated types are covered by the generic types any, data, and simple. When an enumerated object is passed to generically typed formal parameters or in assignments to generically typed field symbols, they inherit the enumerated type. In assignments to field symbols, no castings with the CASTING addition can be performed and an enumerated type cannot be specified after this addition.

When generically typed formal parameters or field symbols are used for enumerated objects, the restriction applies that only statically known operands with the same enumerated type are allowed in read positions in which an enumerated object is expected to be known statically. This affects, for example, the source field of an assignment to an enumerated variable or an operand that is compared with an enumerated object. In write positions for enumerated objects, however, generic formal parameters or field symbols are allowed for enumerated types. If the operand type is not known statically, the check is only made at runtime in read positions as well.

The field symbol fs1 typed generically with simple cannot be assigned to an enumerated variable size known statically or compared with it. An assignment of size to the field symbol and fully generic handling is, however, possible.

Access to the Enumerated Value

A special rule for the conversion operator CONV applies when accessing the enumerated value of an enumerated object:

... CONV base_type( enum_dobj ) ...

If the base type base_type of an enumerated object enum_dobj specified as an argument is specified directly or indirectly, CONV returns its enumerated value.

Conversely, a valid enumerated value can be converted to an enumerated object:

... CONV enum_type( dobj ) ...

The argument dobj is converted to the base type of the enumerated type enum_type and CONV returns an enumerated object with this value. Any invalid values raise an exception.

In combinations of these two variants where CONV base_type( enum_dobj ) is used as an argument dobj of CONV enum_type( dobj ), there is a short form:

... CONV enum_type( enum_dobj ) ...

Provided that different enumerated types can have the same base type, an enumerated object of an enumerated type can be converted to the corresponding enumerated object of a different enumerated type.

Note

The corresponding rules applies to the lossless operator EXACT, whereby additional losslessness checks are made.

The inner conversion operator CONV accesses the current enumerated value of the enumerated object size and returns it in the type i. The outer conversion operator CONV converts the result of the addition back to an enumerated type and assigns this enumerated value to the enumerated variable size. The enumerated value in size is raised by one for each iteration. The final result is the value of the enumerated constant xxl.

Type Descriptions

The type returned by the statement DESCRIBE FIELD for an enumerated object is k. The length is the length of the enumerated values in the basis type in bytes.

In RTTS, enumerated objects are described by objects of the class CL_ABAP_ENUMDESCR. This class can be used for RTTI and RTTC purposes. Like any enumerated type, an enumerated type created using RTTC is only compatible with itself.

The following attributes exist in a type description of the class CL_ABAP_ENUMDESCR:

  • KIND always has the value E for the elementary base type
  • TYPE_KIND always has the value k as in the statement DESCRIBE FIELD
  • BASE_TYPE_KIND describes the base type
  • MEMBERS is a table of the enumerated constants and the associated enumerated values

Enumerated Objects, Type Description

Data Interfaces

The following data interfaces support enumerated types:

The character-like representation of enumerated objects, that is, the result of a c or string, is used for their output and serialization. The output consists of the name with a maximum of thirty characters of the enumerated constant of the current enumerated value in uppercase. Deserializations are performed in the reverse direction. The following are supported:

  • Data clusters with the statements EXPORT and IMPORT. When an enumerated object is exported, the enumerated value is stored in the base type and flagged as an enumerated value. Enumerated objects and exported data objects of the base type can be imported to a suitable enumerated object (the value is checked here). No exported enumerated objects, however, can be imported to data objects of the base type.
  • ABAP file interface with the statements TRANSFER and READ DATASET. In writes and reads to and from a file, enumerated objects are handled like data objects of their base type. In reads from a file to an enumerated object, the system checks whether it is a valid enumerated value.
  • Serializations and deserializations from and to XML and JSON. The formats asXML and asJSON represent the content of enumerated objects in their character-like representation, that is, the name of the enumerated constant of the current enumerated value. Only valid names are allowed in deserializations to an enumerated object.
  • List output with the statement WRITE. Like WRITE TO, this statement converts an enumerated object to its character-like representation, that is, the name of the enumerated constant of the current enumerated value. The output length corresponds to the maximum length of the name of 30 characters.

Note

A deserialization of a name of an enumerated constant is one of the few ways of creating an enumerated value from the name fully dynamically.

Example

WRITE output of the enumerated constants of an enumerated structure.

Enumerated Objects, Deserialization

Forbidden Uses

The following uses are forbidden to ensure that an enumerated object always contains only a single valid enumerated value:

  • Within ABAP, enumerated objects are never interpreted in accordance with their base type. This means that they cannot be used in operand positions that expect numeric, character-like, or byte-like data types. The only exception to this are the operand positions in which an implicit conversion to a character-like type takes place.
  • Enumerated types are not currently supported by the ABAP Dictionary. Accordingly, no database tables whose columns have an enumerated type can be defined in the ABAP Dictionary.
  • In both and Native SQL (EXEC SQL, ADBC), no host variables or references to ABAP variables with enumerated type can be used. The actual parameters of AMDP methods cannot be typed using enumerated types.
  • It is not possible to inherit enumerated values from lists in the list buffer, since there are no conversion rules between character-like types and enumerated types.

If an enumerated object is provided with an invalid value due to a gap in the rules, this results in the following behavior:

  • The result of a conversion to c or string is the string illegal enum value>, which is displayed accordingly in the ABAP Debugger.
  • An enumerated object with an invalid value can be assigned to other enumerated objects with the same enumerated type without being checked.

An invalid value is to be regarded as an error and should never occur.

Note

Classic Dynpros represent a known gap. Enumerated types are not supported by dynpros. If dynpro input fields are inherited from a program by using enumerated objects, they are handled like an object of the base type. Any invalid values are passed to the associated enumerated object at the event PAI without being checked. For this reason, enumerated objects should never be linked with classic dynpros.

Example

The program DEMO_ENUM_DYNPRO uses an enumerated object on a dynpro. This can produce invalid values in the program.






General Material Data   rdisp/max_wprun_time - Maximum work process run time  
This documentation is copyright by SAP AG.

Length: 25772 Date: 20240418 Time: 233526     sap01-206 ( 224 ms )