How to fully specify the type of a returning parameter in method?

Robert McCraw picture Robert McCraw · Mar 8, 2013 · Viewed 15.5k times · Source

I've got a method that imports a structure, creates an internal table out of the structure, and returns this table.

I've implemented it as an exporting method, but now I want to do it as a returning parameter. Part of the idea is that I don't know anything about the structure being passed till runtime so I'm using a fair amount of generics. However, "Returning" methods don't like generics.

method Parameters:
  Importing struct_data TYPE any 
  Returning table_data TYPE STANDARD TABLE 

method STRUCT_TO_TABLE_R.

  FIELD-SYMBOLS:
    <f_fs>    TYPE any,
    <table>   TYPE STANDARD TABLE .

  DO.

    ASSIGN COMPONENT sy-index OF STRUCTURE struct_data TO <f_fs>.

    IF NOT sy-subrc EQ 0.
      EXIT.
    ENDIF.

    APPEND <f_fs> TO <table>.

  ENDDO.

  table_data = <table>.

endmethod.

what do I need to change to fix this?

Answer

C&#233;sar Augusto Scheck Mello picture César Augusto Scheck Mello · Jun 20, 2014

Maybe it's late for a proper response, but I faced the same problem minutes ago.

In order to send an internal table as a RETURNING parameter you need to define a table fully typed in the class. Here is a sample code of a public section of a class:

PUBLIC SECTION.
    TYPES:
      type_table_A TYPE STANDARD TABLE OF T001, //<----- NOT FULLY SPECIFIED
      type_table_B TYPE STANDARD TABLE OF T001 WITH DEFAULT KEY. //<---- FULLY SPECIFIED

In terms of standard table:

TYPE_TABLE_A can be as a parameter only for EXPORTING, IMPORTING and CHANGING parameters.

TYPE_TABLE_B can be used in all possible kinds of parameter in the signature of a method, mainly RETURNING.

So as a good practice, you can decide for declaring fully specified standard table types.

Kind regards,

César Scheck