Inline declarations are a new way of declaring variables and field symbols at operand positions.
In ABAP you have many operand positions, where the value of the operand is changed by the statement. The most typical of these “write positions” is the left hand side lhs of an assignment.
lhs = rhs.
But of course there are more. The data objects you can use at these write positions are either writable formal parameters of the procedure you are working in or variables declared with DATA in front of the statement.
In many cases the variables filled by a statement are helper variables that you only need close to the statement. For each of these helper variables you had to write a data declaration with the DATA statement and of course it was your task to give the variable an adequate type.
Well, the operand type of most write positions is statically fixed and well known to the compiler. And this is why ABAP can offer inline data declarations with Release 7.40. The ingredients are so called declaration positions (write positions with fully known operand type) and the new declaration operator DATA(…).
Let’s look at some examples.
Declaration of a lhs-variable for a simple assignment
DATA text TYPE string.
text = `…`.
DATA(text) = `…`.
Declaration of table work areas
DATA wa like LINE OF itab.
LOOP AT itab INTO wa.
LOOP AT itab INTO DATA(wa).
Declaration of a helper variable
DATA cnt TYPE i.
FIND … IN … MATCH COUNT cnt.
FIND … IN … MATCH COUNT DATA(cnt).
Declaration of a result
DATA xml TYPE xstring.
CALL TRANSFORMATION … RESULT XML xml.
CALL TRANSFORMATION … RESULT XML DATA(xml).
Declaration of actual parameters
DATA a1 TYPE …
DATA a2 TYPE …
oref->meth( IMPORTING p1 = a1
IMPORTING p2 = a2
oref->meth( IMPORTING p1 = DATA(a1)
IMPORTING p2 = DATA(a2)
Declaration of reference variables for factory methods
DATA ixml TYPE REF TO if_ixml.
DATA stream_factory TYPE REF TO if_ixml_stream_factory.
DATA document TYPE REF TO if_ixml_document.
ixml = cl_ixml=>create( ).
stream_factory = ixml->create_stream_factory( ).
document = ixml->create_document( ).
DATA(ixml) = cl_ixml=>create( ).
DATA(stream_factory) = ixml->create_stream_factory( ).
DATA(document) = ixml->create_document( ).
This example is my favorite. When working with class libraries as the iXML-Library you don’t have to care about the data type of the reference variables too much any more. You simply create them inline and use them. As you will see in the 7.40 version of the ABAP Example Library, this feature has facilitated my writings of example programs considerably.
For field symbols there is the new declaration operator FIELD-SYMBOL(…) that you can use at exactly three declaration positions.
ASSIGN … TO FIELD-SYMBOL(<fs>).
LOOP AT itab ASSIGNING FIELD-SYMBOL(<line>).
READ TABLE itab ASSIGNING FIELD-SYMBOL(<line>) …
I guess it is clear to you what happens here.
In my upcoming blogs I will make use of inline declarations when introducing other new features. Be prepared for code like this:
TYPES t_itab TYPE TABLE OF i WITH EMPTY KEY.
DATA(itab) = VALUE t_itab( ( 1 ) ( 2 ) ( 3 ) ).
Yes, this is ABAP 7.40 …