Pass dynamic table as reference out of method

Ever wondered how to get out your inline created dynamic table out of your method / sub procedure?

As known, only references can be passed through a method interface. But I failed always, because the referred table was empty outside of the method (freed stack) because it is created on stack memory which is only valid inside of the method scope.

So I managed to dynamically create a table of the same structure using RTTS and copied the content of the table in there. This table can be assigned to a class attribute reference without loosing the content. Why is ABAP that laborious?

    METHODS get_data.
    METHODS display_alv.
    DATA _r_datatable TYPE REF TO data.
  METHOD get_data.
*   create a fancy dynamic table
    SELECT FROM sflights AS f
      JOIN scarr AS ON c~carrid f~carrid
      FIELDS c~*f~*
      INTO TABLE @DATA(lt_flight). *** copy table to heap ***
* Create table description
    DATA(lo_tabledescCAST cl_abap_tabledescr cl_abap_tabledescr=>describe_by_datap_data lt_flight  ). *  Create table in heap memory and use a reference from outside the method
    CREATE DATA _r_datatable TYPE HANDLE lo_tabledesc.      FIELD-SYMBOLS <lt_datatable> LIKE lt_flight. " Create a field-symbol because append doesn't work with references
    ASSIGN _r_datatable->TO <lt_datatable>. " 
    APPEND LINES OF lt_flight TO <lt_datatable>. " Copy content to heap table

  METHOD display_alv.
    FIELD-SYMBOLS <lt_datatable> TYPE ANY TABLE.
    ASSIGN _r_datatable->TO <lt_datatable>.     cl_salv_table=>factory(
      IMPORTING r_salv_table   DATA(lo_salv)
      CHANGING  t_table        <lt_datatable>
DATA(lo_flightsNEW lc_flights).

Is there any more simple solution for that? Please let me know in the comments.

Please follow my profile.
Read also: