This is a test message to test the length of the message box.
Login
ABAP New Cast Conv
Created by Software-Heroes

ABAP - New, Cast, Conv

9994

Many of the new expressions are so-called constructor expressions. What exactly that means for you and what it brings you find out here in our article.



Constructor expressions or inline functions are functions that can be used in a line of code and also return a result, value or object. They are the opposite of the one-line expressions from the Cobol times, which are still very much in the system.

Take as an example a simple data assignment. This was done by Move and was very easy to read for everyone. With the direct assignment with "=" you can now express this in a single line, saves a lot of typing and is just as easy to read:


DATA:
  ld_one TYPE c LENGTH 10,
  ld_two TYPE c LENGTH 10.

" old assignment
MOVE 'COUNT' TO ld_one.
MOVE ld_one TO ld_two.

" new assignment
ld_two = ld_one = 'COUNT'.

 

Inline functions can now be nested as desired, of course only where it makes sense, and can be used very flexibly. The first three expressions we want to introduce you today.

 

New

The command creates an object of a class and replaces the old "Create Object". The type of the class is implicitly derived from the data type of the target variable, or it is explicitly specified as the usage type. Of course, the second form of the definition saves one line and shows the advantage of the linked functions.


" implicit generation
DATA lo_alv TYPE REF TO cl_gui_alv_grid.
lo_alv = NEW #( ... ).

" explicit generation
DATA(lo_alv) = NEW cl_gui_alv_grid( ... ).

 

Cast

The cast function is used for up/downcasting objects. Previously, it was done with a simple assignment over "=" or the assignment "? =". However, this form of assignment was not always clear and somewhat confusing for some developers. For this purpose they have been implemented this function, which can always be used and shows what happens at this point of the coding. Again, both forms of generation in comparison:


DATA:
  lo_dock TYPE REF TO cl_gui_docking_container,
  lo_cont TYPE REF TO cl_gui_container.

" implicit generation
lo_cont = CAST #( lo_dock ).

" explicit generation
DATA(lo_cont) = CAST cl_gui_container( lo_dock ).

 

In the second case, you do not need the previous definition of lo_cont because you create it at runtime. The Cast command sees the statement at first glance and the developer knows that the type of object has been adjusted here.

 

Conv

The new statement converts a source value into a new target value with a new type and, like all other new expressions, it can also be used concatenated.

Most useful is the function of supplying parameters when they demand a stricter type. Then you can map the variable to the appropriate target type and save yourself the investment of an intermediate variable and the filling of this.


" Conversion 12 -> 0012
DATA(ld_num) = CONV numc4( 12 ).

" Usage as parameter
DATA:
  ld_name TYPE string VALUE 'ZTEST_JOBNAME',
  lt_log  TYPE STANDARD TABLE OF tbtc5.

CALL FUNCTION 'BP_JOBLOG_READ'
  EXPORTING
    jobname   = CONV btcjob( ld_name )
  TABLES
    joblogtbl = lt_log.

In the second example, we read a joblog from the system but only have the job name in a string. However, a call with this parameter would cause the system to dump because the parameter has a different data type. However, with the conversion to the correct data type, we save ourselves the creation of an intermediate variable.

 

Conclusion

With the first new features you get a lot of little helpers on hand, which support your developer life in many ways and make it faster. The direct generation of new variables at runtime also saves a lot of code and makes your coding much clearer.

 

Source:
SAP Documentation New
SAP Documentation Cast
SAP Documentation Conv


Included topics:
New ABAPNewCastConv
Comments (0)



And further ...

Are you satisfied with the content of the article? We post new content in the ABAP area every Friday and irregularly in all other areas. Take a look at our tools and apps, we provide them free of charge.


ABAP - XCO Strings

Category - ABAP

In this article we look at the XCO class for generating and processing strings for ABAP Cloud and compare it with the classic statements.

11/22/2024

ABAP - XCO Libraries

Category - ABAP

What can you do with the library in ABAP and ABAP Cloud and how do you use the objects the best way? Find out more here.

11/12/2024

ABAP - Type Casting

Category - ABAP

How do you actually get the original type of a class or instance if it is passed in a generic table? In this article we examine the possibilities.

04/16/2024

ABAP - RETURN value

Category - ABAP

After all these years, the “real” return in ABAP has finally arrived. In this article we will show you how it works and what it can do.

02/13/2024

ABAP Deep Dive - FOR (Loops)

Category - ABAP

Let's take a closer look at the FOR loop. How does it work? What do I have to consider and what can I do with it?

04/14/2023