0% found this document useful (0 votes)
7 views

ABAP 7.4 and Beyond (4) - Constructor Operators - Discovering ABAP

Uploaded by

m2manas91
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

ABAP 7.4 and Beyond (4) - Constructor Operators - Discovering ABAP

Uploaded by

m2manas91
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

4/21/23, 8:42 PM ABAP 7.

4 And Beyond [4] : Constructor Operators – Discovering ABAP

DISCOVERING ABAP
Knowledge Base for SAP ABAP Developers

ABAP 7.4+, SAP ABAP

ABAP 7.4 And Beyond [4] :


Constructor Operators
Date: September 23, 2021 Author: Jagdish 0

In this post, you will learn about the below constructor operators.

NEW

VALUE

CORRESPONDING

FILTER

COND

SWITCH

CONV

REF

REDUCE

EXACT

NEW – Creates an instance of a class


This operator is used to create an instance of a class.

"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 ( ).

VALUE – Construct variables, internal tables,


and work areas
VALUE is one of the most useful addition in ABAP keywords from ABAP 7.40. With this
keyword, you can create all types of data as shown in the below examples.

TYPES : BEGIN OF ty_user,


user_id TYPE char12,
user_name TYPE text40,
END OF ty_user,

tt_user TYPE STANDARD TABLE OF ty_user WITH EMPTY KEY.


"Structure
DATA(ls_user) = VALUE ty_user( user_id = 'U_JAGD'
user_name = 'Jagdish P' ).

"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 ( ).

CORRESPONDING – Move between structures


and tables.
With this statement, we can move content from one table to another table where table
types are not exactly the same. This can also be used for structures.

TYPES : BEGIN OF ty_base,


a1 TYPE i,
a2 TYPE i,
a3 TYPE char2,
a4 TYPE char10,
END OF ty_base,

BEGIN OF ty_new,
a1 TYPE i,
a2 TYPE i,
b3 TYPE char2,
b4 TYPE i,
END OF ty_new.

DATA : ls_base TYPE ty_base,


ls_new TYPE ty_new,
lt_base TYPE STANDARD TABLE OF ty_base,
lt_new TYPE STANDARD TABLE OF ty_new,
lt_dup TYPE STANDARD TABLE OF ty_base WITH UNIQUE SORTED KEY key1 COMPONENTS a1.

lt_base = VALUE #( ( a1 = 1 a2 = 1 a3 = 'AA' a4 ='One' )


( a1 = 2 a2 = 4 a3 = 'BB' a4 ='Two' )
( a1 = 3 a2 = 9 a3 = 'CC' a4 ='Three' )
( a1 = 4 a2 = 16 a3 = 'DD' a4 ='Four' )
( a1 = 4 a2 = 16 a3 = 'DD' a4 ='Four' ) ). "Duplicate row
"structure
ls_new = CORRESPONDING #( ls_base ).
"table
lt_new = CORRESPONDING #( lt_base ).

"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

FILTER – Moves rows from one table to


another based on the filter condition
You can move the rows which match the where condition or use EXCEPT and move the
rows that do not match the where condition.

DATA messages TYPE SORTED TABLE OF t100 WITH NON-UNIQUE KEY sprsl.
SELECT * FROM t100
WHERE arbgb = 'SABAPDEMOS'
ORDER BY msgnr
INTO TABLE @messages.

"Get all messages in English


DATA(messages_en) = FILTER #( messages WHERE sprsl = 'E' ).
"Get all messages where language is other than English
DATA(messages_non_en) = FILTER #( messages EXCEPT WHERE sprsl = 'E' ).

COND and SWITCH – Conditional Operators


The condition operator will evaluate a condition specified after WHEN and assign the
value specified after THEN. When all the conditions mentioned are false, then the value
specified after ELSE is assigned.

"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

CONV – Type conversion


CONV eliminates the need to use helper variables while passing data to method
parameters or to change and move the value from the field of one type to the field of
another type.

"Old way
DATA : text TYPE c LENGTH 255,
helper TYPE string,
xstr TYPE xstring.

helper = text . "Move to helper variable


xstr = cl_abap_codepage=>convert_to( source = helper ).
"New way
DATA : text TYPE c LENGTH 255.
DATA(xstr) = cl_abap_codepage=>convert_to( source = CONV #( text ) ).

REF – Reference Operator


This is used to get the reference of the data.

"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 ).

REDUCE – Reduction Operator


Reduce creates a result of a specified type using one or more iterations (loop). A simple
example is, to sum up numbers from 1 to 100.

"With implicit data type


DATA(lv_sum) = REDUCE #( INIT s = 0
FOR i = 1 UNTIL i > 100
NEXT s = s + i ).

"With specified data type


DATA(lv_sum) = REDUCE i( INIT s = 0
FOR i = 1 UNTIL i > 100
NEXT s = s + i ).

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

EXACT – Lossless Operator


Exact can be used to assign a variable or expression to the result variable. Here the types
of variables used are usually different. When the lossless assignment can not be done i.e.
data can not be converted – exceptions are triggered. For example –

"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+)

If you like the content, please subscribe…

Type your email… Subscribe

Join 2,982 other followers

#ABAP7.4 #ABAP740 #SAP #SAPABAP

Published by Jagdish

https://discoveringabap.com/2021/09/23/abap-7-4-and-beyond-4-constructor-operators/ 6/10

You might also like