New ABAP expressions for generic and dynamic programming in ABAP Platform 2021 Part 2

Dynamic Structure Component Expressions


A. Introduction to Dynamic Structure Component Expressions:


We introduced several new types of expressions used to dereference data references & dynamically accessing components of structured references.

But there is still a remainder left. Are we not handling with references at all? So if you have a generic structure, for ex: a method parameter of TYPE DATA & want to access a specific component of this structure?

This new expressions provide a new methods to do just that. Yes we should apply them only in situations when you do not know the exact type of the structure at compile time, only the name of the component you want to access.

Ex: of these new Dynamic Structure Component Expressions -

METHODS foo IMPORTING param TYPE data.
...

METHOD foo.
  DATA comp_name TYPE string VALUE `comp`.
  DATA my_object TYPE REF TO my_class.
  ...
  my_object->method( foo-(comp_name) ).
ENDMETHOD.

Yes we can do chaining with all other dynamic expressions introduced.

  my_object->method( foo-(comp_name)->(another_comp)-(yet_another_comp)->* ).

We can also use table selectors, like in the following ex -

FIELD-SYMBOLS <fs> TYPE any.
DATA dyn_key TYPE string VALUE `my_key`.
...
<fs>->('comp_name')-('another_comp_name')->*[ (dyn_key) = 17 ] = 5.

This is also possible to use multiple table selectors in the chain, but not every combination is possible yet. 

Yes we can also use the in more obscure places, for ex: to get a component of a class attribute after a CAST or NEW statement:

data(result) = cast my_class( obj )->attribute-(comp_name)


B. How to use dynamic structure component expressions in ASSIGN


This new expressions can also be used in the ASSIGN statement -

data struc type struc.
field-symbols <fs> type any.
assign struc-('comp') to <fs>.

There is a replacement for this is 

:ASIGN CMPONENT 'COMP' OF STUCTRE struc TO  <FS>.

It latter form is now deprecated. Don't use ASSIGN COMPONENT anymore.

Main reason why ASSIGN COMPONENT is dangerous is a serious limitation. This cannot handle chains containing generic references correctly. Ex as shown below -

  types: begin of test,
           comp type ref to data,
         end of test.

  data var type test.

  create data var-comp type i.

  assign cmponent 'COMP->*' of structure var to field-symbol(<fs>).
  " oh: sy-subrc is 4! Why?

Above ex: the sy-subrc is 4, because COMP is a generic reference at compile-time. It is very spooky, as we are at runtime when the ASSIGN statement is executed. If this COMP would have had TYPE REF TO i, then the sy-subrc would be 0!

As in old ABAP you had to do the following:

types: begin of test,

comp type ref to data,

end of test.

data var type test.

create data var-comp type i.

:assign cmponent 'COMP' of structure var to field-symbol(<fs>).

assign <fs>->* to field-symbol(<fs2>).

 

Compatibility reasons this cannot be changed anymore.

In above ex: works correctly when using the new ASSIGN variant-

  types: begin of test,
           comp type ref to data,
         end of test.

  data var type test.

  create data var-comp type i.
  
  assign var-('COMP->*') to field-symbol(<fs>).
  " sy-subrc is 0! Everything ok!

The new syntax is shorter, more powerful & more consistent with other ASSIGN variants & always yields the correct result, we should abandon ASSIGN COMPONENT in newer ABAP coding in very recent releases.

Yes we can also chain these kinds of expressions inside ASSIGN:

  types: begin of struc,
           comp type ref to data,
         end of struc.

  data var type struc.

  field-symbols <fs>.
  create data var-('comp') type i.
  assign var-('comp')->* to <fs>.

Tip: However that the sy-subrc is only set , if the last dynamic access in the chain fails. If anyone access at the beginning / in the middle fails then a runtime error occurs.

Main reason for this is to not obscure the errors from longer chains by simply setting a sy-subrc. This is defined that ASSIGN only 'inspects' the last element of a chain by setting a sy-subrc. So all the other chain elements are assumed to be correct.

It is somehow similar to ASSIGN with table selectors, where the sy-subrc is only set when a table line is not contained in the internal table, but not if some other error occurs.

Hope This helps, Thank you everyone.

Comments

Popular posts from this blog

EVENTS IN INTERACTIVE REPORTS OF SAP ABAP

SAP ABAP Fresher Resume/ CV Writing Format..

CONCEPTS OF INNER JOIN AND OUTER JOIN in SAP ABAP