Monday, 18 October 2021 18:22

New kinds of ABAP expressions

Written by Kilian Kilger
Rate this item
(0 votes)
“© 2020. SAP SE or an SAP affiliate company. All rights reserved.” “Used with permission of SAP SE”

Do you use the new ABAP expressions like constructor operators or table selectors in your coding? But you often find that when using generic programming, i.e. data types like REF TO DATA, DATA or ANY you fall back to programming style of the 70th? Then the new ABAP platform 2021 (which shipped with kernel 7.85 last week) has some new features to get you clean up your coding.

The main mantra of the new release is: “Get rid of field-symbols!

This is part of a series of multiple blog posts. Please revisit this page as it might point to the sequel in a few weeks or if new topics concerning generic programming in ABAP may arise.

1. The old days: how to handle generic data references classically?

When using non-generic references in ABAP you always could write the following:

DATA foo TYPE REF TO i. ... foo->* = 5.

Here and in the following the CREATE DATA statement or NEW operator has been omitted.

But when using generically typedreferences this was not possible:

DATA foo TYPE REF TO data. ... foo->* = 5. " Syntax error: No dereferencing of generic reference possible

The only possibility to access the variable “foo” would be to use field-symbols.

DATA foo TYPE REF TO data. ... ASSIGN foo->* TO FIELD-SYMBOL(<fs>). <fs> = 5.

This makes the code more ugly and more difficult to read. It also makes dereferencing the reference impossible inside ABAP expressions, as there is no expression variant of ASSIGN. Another disadvantage is the tricky error handling of ASSIGN. You can have subtle bugs when the error handling is forgotten.

2. Dereferencing generic references is now possible: (nearly) everywhere!

We now lifted the above restriction. You can now use the dereferencing operator in most places in ABAP where you can use generically typed ABAP variables. A simple example would be:

DATA foo TYPE REF TO data. ... my_object->meth( foo->* ).

If FOO is the initial reference, then a runtime error will occur, as in the non-generic case. So no error handling is necessary in most cases.

Of course this also works in ABAP SQL like follows:

DATA ref TYPE REF TO data. ... SELECT * FROM T100 INTO TABLE @ref->*.

This however, immediately leads to a new question: The variable REF is a “REF TO DATA”, not a reference to an internal table type.

The latter is not possible in ABAP

Continue reading here
Read 92 times

Leave a comment

Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.