abapfunction-module

Syntax of a functional method call as an FM parameter?


I have the following piece of code.

REPORT ZZY.

CLASS lcl_main DEFINITION FINAL CREATE PRIVATE.
  PUBLIC SECTION.
    CLASS-METHODS:
      convert_to_xstring
        IMPORTING
          i_param1 TYPE i
          i_param2 TYPE i
        RETURNING
          VALUE(rv_result) TYPE xstring,
      main.
ENDCLASS.

CLASS lcl_main IMPLEMENTATION.
  METHOD convert_to_xstring.
  ENDMETHOD.

  METHOD main.
    DATA: lt_binary_tab TYPE STANDARD TABLE OF x.

    DATA(lv_result) = convert_to_xstring( i_param1 = 1 i_param2 = 2 ).

    CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
      EXPORTING
        buffer = lcl_main=>convert_to_xstring(
                   EXPORTING
                     i_param1 = 1
                     i_param2 = 2
                 )
      TABLES
        binary_tab = lt_binary_tab.

  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  lcl_main=>main( ).

A functional method call that is not a part of a function module call can be written like that.

DATA(lv_result) = convert_to_xstring( i_param1 = 1 i_param2 = 2 ).

However when I want to use it exactly as written above

CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
  EXPORTING
    buffer                = lcl_main=>convert_to_xstring( i_param1 = 1 i_param2 = 2 )
  TABLES
    binary_tab            = lt_binary_tab.

I get the following syntax error.

Field "CONVERT_TO_XSTRING(" is unknown. It is neither in one of the specified tables nor defined by a "DATA" statement. "DATA" statement.

It looks like the compiler needs some guidance in this case to distinguish between an attribute and a method. Why would it be ambiguous for the compiler to let such a case without writing EXPORTING?

CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
  EXPORTING
    buffer                = lcl_main=>convert_to_xstring( EXPORTING i_param1 = 1 i_param2 = 2 )
  TABLES
    binary_tab            = lt_binary_tab.

Solution

  • The design of abap is quite bad. There is something like functional method calls, but you can't use it in combination with all commands. For example the WRITE command doesn't work in combination with functional method calls. This seems to be some kind of "partial compatible" with function method calls. I don't know why(maybe the sap dev folks were drunk), but it is just a fact we have to live with.