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.
Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Jagger
  • 10,350
  • 9
  • 51
  • 93
  • 1
    These method invocations are called [functional methods](https://help.sap.com/abapdocu_70/en/ABAPMETHODS_FUNCTIONAL.htm), which is very different from an [inline function or method](https://en.wikipedia.org/wiki/Inline_function), 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? ;-) – vwegert May 23 '16 at 17:27
  • 1
    Isn't then speaking of an [inline](https://scn.sap.com/community/abap/blog/2013/05/23/abap-news-for-release-740--inline-declarations) 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. – Jagger May 23 '16 at 17:35

1 Answers1

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.

Florian
  • 5,918
  • 3
  • 47
  • 86
  • 1
    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. – Jagger May 27 '16 at 13:16
  • 1
    I would guess, it's some kind of a "bug". But nobody knows, in the world of sap... – Florian May 27 '16 at 13:22