Syntax of a functional method call as an FM parameter?
Asked Answered
P

1

6

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.
Pantomimist answered 23/5, 2016 at 17:4 Comment(2)
These method invocations are called functional methods, which is very different from an inline function or method, which AFAIK doesn't exist in ABAP. I'd rather not confuse the two. And, as for your question - CALL FUNCTION is a mess - is that enough of an answer? ;-)Stinkpot
Isn't then speaking of an inline declaration just as confusing? I will rephrase though, because this way of method call has been indeed named functional from the very beginning. I remember inline function in C++, they are good alternative for preprocessor macros.Pantomimist
I
4

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.

Inaccurate answered 26/5, 2016 at 10:35 Comment(2)
Yep, I agree but in this case it is possible to use a functional method, only it has to be done with a "hack". I am wondering what would be the possible ambiguity here if the EXPORTING needn't have to be used.Pantomimist
I would guess, it's some kind of a "bug". But nobody knows, in the world of sap...Inaccurate

© 2022 - 2024 — McMap. All rights reserved.