This is a list of new language features in a NW 7.40 system, compared with a 6.20 system.
NEW Operator for Objects
In 7.40, creating an object required a lengthy statement. This has become shorter:
old:
DATA lo_object TYPE REF TO <an interface>.
CREATE OBJECT lo_object TYPE <a class> EXPORTING iv_parameter = 'a value'.
new:
DATA lo_object TYPE REF TO <an interface>.
lo_object = NEW <a class>( iv_parameter = 'a value' ).
It is even possible to create an anonymous object without declaring a variable for it:
NEW object-><a method>( ).
There is a shortcut which combines declaration and assignment:
DATA(lo_object> = NEW <a class>( iv_parameter = 'a value' ).
In this case, the object type of variable "lo_object" is derived from the NEW operator. The object type is the class "<a class>".
There is another shortcut where the constructor is derived from the declared type. The hashmark denotes the class type infered from the data deeclaration:
DATA lo_object TYPE REF TO <a class>.
lo_object = NEW #( ).
Here, the constructor called is the constructor from the static type "<a class>".
NEW Operator for Primitive Data
The NEW operator can be applied to data of primitive types as well. The following looks like auto-boxing in Java. Caution: The access is via reference:
old:
DATA lr_integer TYPE REF TO i.
CREATE DATA lr_integer.
lr_integer->* = 123.
WRITE: /, 'integer value:', lr_integer->*.
new:
DATA(lr_integer) = NEW i( 123 ).
WRITE: /, 'integer value:', lr_integer->*.
The code creates a new integer number intialized with value 123.
There are shortcuts for data creation as well. The hashmark # denotes the data type infered from the variable declaration.:
DATA lr_integer TYPE REF TO i.
lr_integer = NEW #( 123 ).
DATA(lr_string) = NEW string( 'Hello' ).
WRITE: /, 'string value: ', lr_string->*, ', integer value: ', lr_integer->*.
NEW Operator for Structures and Tables
One can create a new structure and initialize some fields of the structure. The fields are set by fieldname. The access is via reference:
TYPES BEGIN OF t_struct,
col1 TYPE i,
col2 TYPE string,
END OF t_struct.
DATA lr_structure TYPE REF TO t_struct.
lr_structure = NEW t_struct( col1 = 123 col2 = 'Hello' ).
There are shortcuts for creation as well.
DATA lr_structure TYPE REF TO t_struct.
lr_structure = NEW #(col1 = 123 col2 = 'Hallo ).
The NEW operator for tables is analogous. The access is via reference.
TYPES t_table TYPE TABLE of t_struct.
DATA lr_table TYPE REF TO t_table.
lr_table = NEW t_struct( ( col1 = 123 col2 = 'Hello' )
( col1 = 456 col2 = 'Goodbye' )).
The example creates a table with two rows.
Inline Declarations of Variables
It is possible to declare variables inline within an ABAP statement. The type is inferred from the statement.
DATA(lv_integer) = new i( 123 ).
LOOP AT lt_table INTO DATA(ls_line).
...
ENDLOOP.
LOOP AT lt_table ASSIGNING FIELD-SYMBOL(<ls_line>).
...
ENDLOOP.
The two variables lv_integer and ls_line and the field symbol <ls_line> are declared inline.
Caution: ABAP does not know a local scope. I.e. the variable ls_line is NOT ONLY known within the loop. It is known within the whole method.
Creation of Anonymous Complex Data Objects with VALUE
This concept is similar to object literals in JavaScript. A new anonymous data literal is created with keyword VALUE.
TYPES BEGIN OF t_struct,
col1 TYPE i,
col2 TYPE string, END OF t_struct.
DATA ls_structure TYPE t_struct.
ls_structure = VALUE t_struct( col1 = 123 col2 = 'Hello' ).
Here, an anonymous data object initialized with values for col1 and col2 is created and assigned to variable "ls_structure".
Unlike the use of NEW operator with complex data, we do not use a reference to the data, we use the data literal directly.
There are shortcuts for creation. The hashmark # can be used in lieu of an explicit data type if the type can be derived from the assignment.
TYPE t_table TYPE TABLE of t_struct.
DATA lt_table TYPE t_table.
lt_table = VALUE #( ( col1 = 123 col2 = 'Hello' )
( col1 = 456 col2 = 'Goodbye' )).
Especially, an anonymous data object can used if a method has a mandatory importing parameter. The data type is derived from the method signature:
DATA lo_object TYPE REF TO <a class>.
lo_object->set_structure( VALUE #( ) ).
In this example, the object's method "set structure" requires an importing parameter of some complex data type. The operator "VALUE #( )" derives the needed data type from the method signature and creates a matching anyonymous object.
Host Variables in OPENSQL Statements
Previously, the ABAP language and the OPENSQL language were closely intertwined. Especially, an ABAP variable declared in an ABAP statement could be directly used in an OPENSQL statement.
In NW 7.40, the languages try to distinguish between variables in ABAP and in OPENSQL.
old:
DATA ls_sflight TYPE sflight.
SELECT SINGLE * FROM sflight INTO ls_sflight.
new:
DATA ls_sflight TYPE sflight.
SELECT SINGLE * FROM sflight INTO @ls_sflight.
With the new syntax it is more obvious that a variable declared in ABAP is used within an OPENSQL statement.
Regular Expressions With FIND and REPLACE
It is possible to use regular expressions with FIND and REPLACE:
FIND <regex> IN <a string>.
REPLACE <regex> IN <a string> WITH <new>.
String Operations
No need to use the obnoxious CONCATENATE! One can use '&&' instead:
DATA lv_string TYPE string.
lv_string = 'abc' && 'def' && 'ghi'.
There are new string functions like substring, insert, match, replace, to_upper, to_lower:
DATA lv_substring TYPE string.
lv_substring = substring( val = 'abcdefghi' off = 2 len = 3 ).
String Templates
A string template can be used to create a string from one or more expressions.
Example:
DATA lv_text TYPE string.
lv_text = |{ lo_object1->to_string( ) } { lo_object2->to_string( ) }|.
The string template is embedded in '|'.
Each expression is embedded in curly braces.
Boolean Functions
The function BOOLC can be used to evaluate a Boolean expression. It returns the usual characters 'X' or ' '.
DATA lv_flag TYPE c.
lv_flag = boolc( sy-batch IS NOT INITIAL ).