ABAP Built-In Generic Data Types
The figure illustrates the predefined generic data types supported by ABAP syntax.
In addition to complete data types, ABAP also features a number of generic data types. In contrast to complete types, you cannot use generic types to define the properties of data objects. You only use generic types to type formal parameters and field symbols.
Note: Currently, the two built-in types, ... TYPE REF TO OBJECT and ... TYPE REF TO DATA are the only types available for typing reference variables generically.
Generic ABAP Data Types – Internal Tables
Most generic types, such as clike, csequence, numeric, simple, and xsequence, are available as of SAP Web AS 6.10. Generic type decfloat is available as of SAP NetWeaver 7.0 EhP 2.
Each of the generic types is compatible with the set of ABAP types shown in the previous figures. When you call a procedure, the system checks whether the type of the actual parameter is compatible with the type of the formal parameter. When you assign a generically typed field symbol, the syntax check tests whether the type of the data object is compatible with the generic type of the field symbol.
The DATA type behaves in the same way as ANY. The differentiation is purely theoretical at this point. In the future, however, reference variables with type TYPE REF TO ANY to point to data objects or instances of classes might be possible.
The ANY TABLE, INDEX TABLE, and SORTED TABLE are special generic data types that are only compatible with internal tables.
Freely Defined Generic Table Types
In addition to these built-in generic table types, ABAP also contains generic table types that developers create as generic types. These generic table types are created either in the ABAP Dictionary or in the program source code with the TYPES statement.
The figure shows an example of locally defined generic table types.
In these generic table types, you define the line type, but the table type and key definition remain open.
Note: To create appropriate generic types in the ABAP Dictionary, check the corresponding options under the Access and Key tabs.
Generically Typed Parameters
When you generically type formal parameters of a procedure, such as a subroutine, function module, or method, the procedure becomes more flexible. Use the syntax check to ensure that the procedure handles the different types.
The syntax check ensures that the system passes only actual parameters with compatible types to generically typed formal parameters. However, when generically typed parameters are addressed in ABAP statements, you often cannot determine whether the type of the underlying data object or its current content is logical in the respective operand position until runtime. If conflicts occur, the system raises runtime errors that cannot be caught in most cases. The figure shows an example.
If you assign an internal table or a structure that is not flat and character-type to the parameter in the example, the WRITE statement raises an uncatchable exception, OBJECTS_NOT_CHARCONV.
Type Precisely to Avoid Errors
If you use the generic type SIMPLE in the example shown in the figure, the WRITE statement does not cause runtime errors. You can convert all data types that are compatible with SIMPLE to a character string.
The syntax errors are corrected because parameter it_data now accepts only internal tables because of its type, ANY TABLE.
Hint: A runtime error can still occur if the method contains an index access to it_data, specifically, if the actual parameter is a hashed table. Use the generic type INDEX TABLE to restrict the formal parameter even further.
Generically Typed Field Symbols
Field symbols are pointers that can be assigned to data objects dynamically. When the field symbol is used in an ABAP statement, you can dynamically define the specific data object that the field symbol refers to.