Search code examples
oopabaprtti

How to compare two RTTI type descriptors equality?


I would like to know if two RTTI objects describe the same type.

Is it safe to assume that two objects describing the same type would reference the same instance ?

IF lo_typedescr1 = lo_typedescr2.
  " Described types are identical
ELSE.
  " Described types are not identical
ENDIF.

Solution

  • The answer depends on the exact context in which you are comparing two ABAP types, and on which criteria you use to consider that the types are identical.

    Here is an example which demonstrates that there are two different RTTI Objects (which means that the condition IF type_1 = type_2 is evaluated as false) for the "same" type, one is declared using TYPE c LENGTH 1 and another one using TYPE flag which is an ABAP Dictionary data element of type C and length 1:

    REPORT.
    DATA type_1 TYPE REF TO cl_abap_typedescr.
    DATA type_2 TYPE REF TO cl_abap_typedescr.
    PERFORM get_type_1 CHANGING type_1.
    PERFORM get_type_2 CHANGING type_2.
    ASSERT 1 = 1. " <== debug place to look at RTTI Object References type_1 and type_2
    FORM get_type_1 CHANGING type_1 TYPE REF TO cl_abap_typedescr.
      DATA one_character TYPE c LENGTH 1.
      type_1 = cl_abap_typedescr=>describe_by_data( one_character ).
    ENDFORM.
    FORM get_type_2 CHANGING type_2 TYPE REF TO cl_abap_typedescr.
      DATA one_character TYPE flag. " ABAP Dictionary CHAR 1
      type_2 = cl_abap_typedescr=>describe_by_data( one_character ).
    ENDFORM.
    

    By debug we see that the RTTI Objects are different: Different RTTI Objects - ADT debugger

    Here is another example which also demonstrates that there are two different RTTI Objects for the "same" structured type:

    • In get_type_1:
      DATA: BEGIN OF structure,
              comp1 TYPE c LENGTH 1,
            END OF structure.
      type_1 = cl_abap_typedescr=>describe_by_data( structure ).
      
    • In get_type_2:
      DATA: BEGIN OF structure,
              comp1 TYPE c LENGTH 1,
            END OF structure.
      type_2 = cl_abap_typedescr=>describe_by_data( structure ).
      

    In the blog post Improve the Memory Consumption of ABAP Runtime Type Creation (RTTC), we can see that the dynamic creation of types with the method create may create several RTTI Objects corresponding to the exact same type.

    • Here it doesn't work with the method create:
      • In get_type_1:
        type_1 = cl_abap_structdescr=>create( VALUE #( ( name = 'COMP1' type = cl_abap_elemdescr=>get_c( 1 ) ) ) ).
        
      • In get_type_2:
        type_2 = cl_abap_structdescr=>create( VALUE #( ( name = 'COMP1' type = cl_abap_elemdescr=>get_c( 1 ) ) ) ).
        
    • Here it works well with the method get:
      • In get_type_1:
        type_1 = cl_abap_structdescr=>get( VALUE #( ( name = 'COMP1' type = cl_abap_elemdescr=>get_c( 1 ) ) ) ).
        
      • In get_type_2:
        type_2 = cl_abap_structdescr=>get( VALUE #( ( name = 'COMP1' type = cl_abap_elemdescr=>get_c( 1 ) ) ) ).
        

    The official ABAP documentation - Runtime Type Services (RTTS) is helpless because it describes RTTS in only a few sentences.

    Of course, people will often find that the test IF type_1 = type_2 will work, but that won't be always the case as I explained above.

    To be complete, with this example below, IF type_1 = type_2 is evaluated as true:

    • In get_type_1:
      DATA v1 TYPE c LENGTH 1.
      type_1 = cl_abap_typedescr=>describe_by_data( v1 ).
      
    • In get_type_2:
      DATA v2 TYPE c LENGTH 1.
      type_2 = cl_abap_typedescr=>describe_by_data( v2 ).
      
    • Debug: Identical RTTI Object - ADT debugger