ABAP 7.4 and Beyond (4) - Constructor Operators - Discovering ABAP
ABAP 7.4 and Beyond (4) - Constructor Operators - Discovering ABAP
DISCOVERING ABAP
Knowledge Base for SAP ABAP Developers
In this post, you will learn about the below constructor operators.
NEW
VALUE
CORRESPONDING
FILTER
COND
SWITCH
CONV
REF
REDUCE
EXACT
"Old way
DATA: lo_abap TYPE REF TO lcl_abap.
CREATE OBJECT lo_abap
https://discoveringabap.com/2021/09/23/abap-7-4-and-beyond-4-constructor-operators/ 1/10
4/21/23, 8:42 PM ABAP 7.4 And Beyond [4] : Constructor Operators – Discovering ABAP
"New way
DATA(lo_abap) = NEW lcl_abap( ).
When the class has a constructor with parameters, then we can pass the parameters
within the brackets ( ).
"Internal table
DATA(lt_itab) = VALUE tt_user(
( user_id = 'U_PATJAG' user_name = 'Jagdish P' )
( user_id = 'U_DOEJOH' user_name = 'John Doe' )
( user_id = 'U_DOEJAN' user_name = 'Jane Doe' )
).
"Nested structure
TYPES : BEGIN OF ty_addr,
house_number TYPE numc4,
street TYPE text20,
city TYPE text20,
zip_code TYPE text20,
END OF ty_addr,
BEGIN OF ty_customer,
name TYPE text40,
address TYPE ty_addr,
date_of_birth TYPE sy-datum,
END OF ty_customer.
DATA(john_mayer) = VALUE ty_customer(
name = 'John Doe'
address = VALUE #(
house_number = '001'
street = 'Unique Road Name'
city = 'Some New City'
zip_code = '124421'
)
date_of_birth = '19750101'
).
"Range
DATA lt_range TYPE RANGE OF land1.
lt_range = VALUE #(
sign = 'I' option = 'EQ'
( low = 'IN' )
https://discoveringabap.com/2021/09/23/abap-7-4-and-beyond-4-constructor-operators/ 2/10
4/21/23, 8:42 PM ABAP 7.4 And Beyond [4] : Constructor Operators – Discovering ABAP
( low = 'US' )
( low = 'UK' )
( low = 'FR' )
).
The # used after the keyword VALUE along with inline data declaration. This means the
TYPE of the variable or fields will be determined implicitly or from the earlier declaration.
If you want to use a specific type, use the type in place of #.
In the range construction, as sign = 'I' and option = 'EQ' are common for all the
entries, hence they are specified only once at the top outside the inner ( ).
BEGIN OF ty_new,
a1 TYPE i,
a2 TYPE i,
b3 TYPE char2,
b4 TYPE i,
END OF ty_new.
"with mapping of fields with different field name and skip some fields
lt_new = CORRESPONDING #( lt_base MAPPING b4 = a1
EXCEPT a2 ).
"Handling duplicates
lt_dup = CORRESPONDING #( lt_base DISCARDING DUPLICATES ).
https://discoveringabap.com/2021/09/23/abap-7-4-and-beyond-4-constructor-operators/ 3/10
4/21/23, 8:42 PM ABAP 7.4 And Beyond [4] : Constructor Operators – Discovering ABAP
DATA messages TYPE SORTED TABLE OF t100 WITH NON-UNIQUE KEY sprsl.
SELECT * FROM t100
WHERE arbgb = 'SABAPDEMOS'
ORDER BY msgnr
INTO TABLE @messages.
"COND
DATA(lv_result) = COND #( WHEN sy-msgty = 'S' THEN 'Success'
WHEN sy-msgty = 'W' THEN 'Warning'
WHEN sy-msgty = 'I' THEN 'Information'
WHEN sy-msgty = 'A' THEN 'Abort'
ELSE 'Undefined' ).
SWITCH is similar to COND but it uses a single variable and works similarly to a CASE
statement in conventional ABAP.
"SWITCH
DATA(lv_result) = SWITCH #( sy-msgty WHEN 'S' THEN 'Success'
WHEN 'W' THEN 'Warning'
WHEN 'I' THEN 'Information'
WHEN 'A' THEN 'Abort'
ELSE 'Undefined' ).
COND # will let you write more complex conditions than SWITCH, but when you have only
one variable, SWITCH is earlier to write and understand.
https://discoveringabap.com/2021/09/23/abap-7-4-and-beyond-4-constructor-operators/ 4/10
4/21/23, 8:42 PM ABAP 7.4 And Beyond [4] : Constructor Operators – Discovering ABAP
"Old way
DATA : text TYPE c LENGTH 255,
helper TYPE string,
xstr TYPE xstring.
"Old way
TYPES: tt_flights TYPE STANDARD TABLE OF sflight WITH EMPTY KEY.
DATA : it_flights TYPE tt_flights,
dref TYPE REF TO tt_flights.
SELECT * FROM sflight INTO TABLE it_flights.
GET REFERENCE OF it_flights INTO dref.
"New way
SELECT * FROM sflight INTO TABLE @DATA(it_flights).
DATA(dref) = REF #( it_flights ).
https://discoveringabap.com/2021/09/23/abap-7-4-and-beyond-4-constructor-operators/ 5/10
4/21/23, 8:42 PM ABAP 7.4 And Beyond [4] : Constructor Operators – Discovering ABAP
"Conversion error
TYPES numtext TYPE n LENGTH 255.
TRY.
DATA(number) = EXACT numtext( '4 Apples + 2 Oranges' ).
CATCH cx_sy_conversion_error INTO DATA(exc).
Message 'Exception cx_sy_conversion_error is triggered' Type 'E'
ENDTRY.
"rounding loss
TRY.
DATA(exact_result) = EXACT #( 3 * ( 1 / 3 ) ).
CATCH cx_sy_conversion_rounding INTO DATA(lo_exc).
DATA(rounded_result) = lo_exc->value.
ENDTRY.
Now, you should be able to use these constructor operators to write codes quicker and
better.
Read about more such ABAP expressions and exciting new syntaxes: ABAP
Expressions (7.4+)
Published by Jagdish
https://discoveringabap.com/2021/09/23/abap-7-4-and-beyond-4-constructor-operators/ 6/10