Skip to Content
avatar image
-1
Former Member

Method chaining

Got a simple question on method chaining. I understand this is not a new topic and there are several threads showing examples on how to chain methods but there seems to be a small thing that makes understanding of this concept a little difficult.

I am using the class CL_SALV_TABLE where in the variable ALV is an instance of the class CL_SALV_TABLE.

While chaining, we use the below code.

alv->get_columns( )->set_optimize( ).

Now the method "get_columns" of the instance ALV returns a parameter of type CL_SALV_COLUMNS_TABLE thru which we call the method "set_optimize".

Question -> for the method call "get_columns" the returning parameter is NOT OPTIONAL. So what exactly happens behind the scenes which allow us to skip the helper variable? The instance of the variable ALV and the return parameter of the method "get_columns" are not of the object.

Can some help me understand the concept behind this?

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

4 Answers

  • Jul 25, 2017 at 02:41 PM

    This type of method call is called a 'Functional Method' and as such can be used directly in assignments, conditions etc etc. It cannot be used for example as an argument to a function module.

    There is nothing strange about things happening behind the scenes - it's a parameter with a 'Returning' rather than a 'Changing' or 'Exporting' type.

    You would define it in a local class something like:

     Close_Notification         Final
                                Importing i_Notif_No              Type Notification_Number
                                Returning Value(r_State)          Type Notification_State,

    And specify it in SE80 basically the same way.

    Add comment
    10|10000 characters needed characters exceeded

  • Jul 25, 2017 at 02:48 PM

    Just consider that alv->get_columns( ) to be the returned parameter/object. (or read your course on OO functional syntax)

    Add comment
    10|10000 characters needed characters exceeded

  • Jul 25, 2017 at 03:12 PM
    Add comment
    10|10000 characters needed characters exceeded

  • Jul 27, 2017 at 08:14 AM

    Your statement

    "Now the method "get_columns" of the instance ALV returns a parameter of type CL_SALV_COLUMNS_TABLE thru which we call the method "set_optimize"."

    is not true.

    Method "get_columns" of the instance ALV returns a parameter of type REF TO CL_SALV_COLUMNS_TABLE.

    Your Question:

    1. alv->get_columns( )->set_optimize( ) is evaluated from left side to right
    2. alv->get_columns( ) yields a reference to an CL_SALV_COLUMNS_TABLE object
    3. message "set_optimize" is sent to this CL_SALV_COLUMNS_TABLE object
    4. CL_SALV_COLUMNS_TABLE object provides method set_optimize( ) to react on message "set_optimize"
    Add comment
    10|10000 characters needed characters exceeded