Search code examples
return-valueabapdereference

Inline dereferencing method return parameters


I've seen several ABAP standard methods that return a reference to data as result. CL_ABAP_EXCEPTIONAL_VALUES=>GET_MAX_VALUE( ) is one of those methods. My natural inclination is to use this method in a single line, like this:

DATA lv_max_value TYPE i.
lv_max_value = CL_ABAP_EXCEPTIONAL_VALUES=>GET_MAX_VALUE( lv_max_value )->*.

Sadly, this doesn't work, because:

Result type of the functional method "GET_MAX_VALUE" is not an object reference or an interface reference.

The question at hand is: is it possible to dereference such results directly?

Whenever I am certain that results are compatible I would prefer to avoid the old method of dereferencing (storing the reference, assigning it to a field-symbol and then putting it into destination variable) :

DATA lv_max_value TYPE i.
DATA ref TYPE REF TO data.
FIELD-SYMBOLS <field> TYPE any.
ref = CL_ABAP_EXCEPTIONAL_VALUES=>GET_MAX_VALUE( lv_max_value ).
ASSIGN ref->* TO <field>.
lv_max_value = <field>.

It seems like a massive operation for a simple action.


Solution

  • The method GET_MAX_VALUE returns a variable typed TYPE REF TO DATA which is a "reference to a generic data type".

    You cannot dereference generic references (*).

    However, you can first CAST them, to make ABAP aware of the concrete data type, then dereference the (now typed) result of the cast.

    DATA lv_max_value TYPE i.
    lv_max_value = CAST i( cl_abap_exceptional_values=>get_max_value( lv_max_value ) )->*.
    

    (*) The documentation of TYPES - REF TO says that only references to complete data types can be dereferenced:

    A data reference variable typed in full with TYPE REF TO complete_type or LIKE REF TO dobj can be dereferenced in all matching operand positions using the dereferencing operator ->*. If the static data type is structured, the object component selector enables access to the components of the structure with dref->comp.

    and this documentation explains that a complete data type is a "Data type that is not generic."