ABAP 7.4 Syntax
ABAP 7.4 Syntax
Jangid, Aditya
ABAP 7.4
By Aditya
Expression Warm Up
Example 1.
DATA itab TYPE TABLE OF scarr.
cl_demo_output=>display( output ).
inline declarations in declaration positions. Let’s rewrite the above code in 7.40
Gee! Square brackets within curly brackets and no READ-statement any more 😉
1
Inline Declarations
Inline declarations are a new way of declaring variables and field symbols at operand positions.
Before 7.40
With 7.40
With 7.40
With 7.40
Declaration of a result
Before 7.40
DATA xml TYPE xstring.
CALL TRANSFORMATION … RESULT XML xml.
With 7.40
2
Declaration of actual parameters
Before 7.40
DATA a1 TYPE …
DATA a2 TYPE …
oref->meth( IMPORTING p1 = a1
IMPORTING p2 = a2
… )
With 7.40
ixml = cl_ixml=>create( ).
stream_factory = ixml->create_stream_factory( ).
document = ixml->create_document( ).
With 7.40
DATA(ixml) = cl_ixml=>create( ).
DATA(stream_factory) = ixml->create_stream_factory( ).
DATA(document) = ixml->create_document( ).
3
Field Symbols:
For field symbols there is the new declaration operator FIELD-SYMBOL(<Field_symbol>) that you can use at
exactly three declaration positions.
4
Constructor Operator NEW
7.40 ABAP supports constructor operators.
Constructor operators are used in constructor expressions to create a result that can be used at operand positions. The syntax
for constructor expressions is
operator <datatype>/#( )
operator is a constructor operator. type is either the explicit name of a data type or the character #. With # the data type can be
derived from the operand position if the operand type is statically known. Inside the parentheses specific parameters can be
specified.
creates an anonymous data object of data type dtype and passes a value to the created object. The value construction
capabilities cover structures and internal tables (same as those of the VALUE operator).
… NEW class( p1 = a1 p2 = a2 … ) …
creates an instance of class class and passes parameters to the instance constructor.
… NEW #( … ) …
creates either an anonymous data object or an instance of a class depending on the operand type.
You can write a component selector -> directly behind NEW type( … ).
Example for data objects
Before Release 7.40
5
Example for instances of classes
Before Release 7.40
This is the kind of statement NEW is made for. You can also pass it to methods expecting references.
BEGIN OF t_struct2,
col1 TYPE i,
col2 TYPE t_struct1,
col3 TYPE TABLE OF t_struct1 WITH EMPTY KEY,
END OF t_struct2,
6
Constructor Operator VALUE
The value operator VALUE is a constructor operator that constructs a value for the type specified with type.
VALUE dtype|#( ( … ) ( … ) … ) …
constructs an internal table, where for each line a value can be assigned. Inside inner parentheses you can use
the syntax for structures but not the syntax for table lines directly. But you can nest VALUE operators.
Note that you cannot construct elementary values (which is possible with instantiation operator NEW) – simply
because there is no need for it.
For internal tables with a structured line type there is a short form that allows you to fill columns with the same
value in subsequent lines
VALUE dtype|#( col1 = dobj11 … ( col2 = dobj12 col3 = dobj13 … )
( col2 = dobj22 col3 = dobj23 … )
…
col1 = dobj31 col2 = dobj32 … ( col3 = dobj33 … )
( col3 = dobj43 … )
… ).
CLASS c1 DEFINITION.
PUBLIC SECTION.
TYPES: BEGIN OF t_struct,
col1 TYPE i,
col2 TYPE i,
END OF t_struct.
CLASS-METHODS m1 IMPORTING p TYPE t_struct.
ENDCLASS.
CLASS c1 IMPLEMENTATION.
METHOD m1.
…
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
c1=>m1( VALUE #( ) ).
7
Example for structures
“1
struct = VALUE t_struct( col1 = 1
col2-col1 = 1
col2-col2 = 2 ).
“2
col2 = VALUE t_col2( col1 = 1
col2 = 2 ).
“3
struct = VALUE t_struct( col1 = 1
col2 = VALUE #( col1 = 1
col2 = 2 ) ).
8
Examples for internal tables:
Elementary line type:
TYPES t_itab TYPE TABLE OF i WITH EMPTY KEY.
itab = VALUE #( ( ) ( 1 ) ( 2 ) ).
9
Constructor Operator REF
The reference operator REF constructs a data reference at operand positions.
… REF dtype|#( dobj ) …
results in a data reference pointing to dobj with the static type specified by type. with other words, REF is the short form
for GET REFERENCE OF dobj INTO.
Example for dynamic method call
START-OF-SELECTION.
DATA(arg1) = `blah`.
DATA(arg2) = 111.
DATA(result) = sql->execute_query(
`SELECT carrname ` &&
`FROM scarr ` &&
`WHERE mandt = ? AND carrid = ?` ).
10
Operators CONV and CAST
The conversion operator CONV is a constructor operator that converts a value into the type specified in type.
… CONV dtype|#( … ) …
You use CONV where you needed helper variables before in order to achieve a requested data type.
helper = text.
You use CAST for a down cast where you needed helper variables before in order to cast with ?=to a
requested reference type.
You use CAST for an up cast, e,g, with an inline declaration, in order to construct a more general type.
You can write a compnent selector -> directly behind CAST type( … ).
Example from RTTI
Common example where a down cast is needed.
11
With release 7.40
DATA(components) = CAST cl_abap_structdescr(
cl_abap_typedescr=>describe_by_name( ‘T100’ ) )->components.
Example with up cast
The static type of the reference variable iref declared inline should be the interface not the class.
INTERFACE if.
…
ENDINTERFACE.
CLASS cl DEFINITION CREATE PRIVATE.
PUBLIC SECTION.
INTERFACES if.
CLASS-METHODS factory RETURNING value(ref) TYPE REF TO cl.
…
ENDCLASS.
CLASS cl IMPLEMENTATION.
METHOD factory.
ref = NEW #( ).
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA(iref) = CAST if( cl=>factory( ) ).
Example with data objects
A constructor expression with CAST followed by -> is an LHS-expression, you can assign values to it.
TYPES: BEGIN OF t_struc,
col1 TYPE i,
col2 TYPE i,
END OF t_struc.
DATA dref TYPE REF TO data.
DATA struc TYPE t_struc.
dref = NEW t_struc( ).
CAST t_struc( dref )->col1 = struc–col1.
12
Constructor Operator EXACT
Lossless Operator EXACT
The lossless operator EXACT is a constructor operator that exceutes either a lossless calculation or a lossless
assignment.
Lossless calculations
Lossless calculations were introduced for decimal floating point numbers in Release 7.02 with the
addition EXACT to COMPUTE. This addition (better the whole statement COMPUTE) became obsolete now.
A lossless calculation must not perform any roundings. If it does, an exception occurrs.
Example
TRY.
DATA(r1) = EXACT decfloat34( 3 / 2 ).
cl_demo_output=>write( |Exact: { r1 }| ).
CATCH cx_sy_conversion_rounding INTO DATA(e1).
cl_demo_output=>write( |Not exact: { e1->value }| ).
ENDTRY.
TRY.
DATA(r2) = EXACT decfloat34( 3 / 7 ).
cl_demo_output=>write( |Exact: { r2 }| ).
CATCH cx_sy_conversion_rounding INTO DATA(e2).
cl_demo_output=>write( |Not exact: { e2->value }| ).
ENDTRY.
cl_demo_output=>display( ).
You see that the non-exact result can be found in the exception object.
13
Lossless assignments
Lossless assignments were introduced for conversions in Release 7.02 with the addition EXACT to MOVE. This
addition (better the whole statement MOVE) became obsolete now.
A lossless assignment is an assignment where
Example
TYPES numtext TYPE n LENGTH 10.
The result is
0000000042
The argument ‘4 Apples + 2 Oranges’ cannot be interpreted as a number
The infamous conversion rule from c to n is not supported by EXACT.
14
Operators COND and SWITCH
Last but not least, two nice ones, the conditionals COND and SWITCH.
DATA(time) =
COND string(
WHEN sy-timlo < ‘120000’ THEN
|{ sy-timlo TIME = ISO } AM|
WHEN sy-timlo > ‘120000’ THEN
|{ CONV t( sy-timlo – 12 * 3600 )
TIME = ISO } PM|
WHEN sy-timlo = ‘120000’ THEN
|High Noon|
ELSE
THROW cx_cant_be( ) ).
Note the THROW. Now you can raise and throw exceptions …
15
Example for SWITCH
CLASS cx_langu_not_supported DEFINITION INHERITING FROM cx_static_check.
ENDCLASS.
CLASS class DEFINITION.
PUBLIC SECTION.
METHODS meth IMPORTING iso_langu TYPE string
RETURNING VALUE(text) TYPE string.
ENDCLASS.
CLASS class IMPLEMENTATION.
METHOD meth.
…
ENDMETHOD.
ENDCLASS.
…
DATA(text) =
NEW class(
)->meth(
SWITCH #( sy-langu
WHEN ‘D’ THEN `DE`
WHEN ‘E’ THEN `EN`
ELSE THROW cx_langu_not_supported( ) ) ).
16
17