Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
horst_keller
Product and Topic Expert
Product and Topic Expert

With Release 7.40 ABAP supports so called 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 type( ... ) ...

operator is a constructor operator. type is either the explicit name of a data type or the character #. With # the data type can be dreived from the operand position if the operand type is statically known. Inside the parentheses specific parameters can be specified.

Conversion Operator CONV

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.

Example for parameter passing

Method cl_abap_codepage=>convert_to expects a string but you want to convert a text field.

Before release 7.40

DATA text TYPE c LENGTH 255.

DATA helper TYPE string.

DATA xstr   TYPE xstring.

helper = text.

xstr = cl_abap_codepage=>convert_to( source = helper ).

With release 7.40

DATA text TYPE c LENGTH 255.

DATA(xstr) = cl_abap_codepage=>convert_to( source = CONV string( text ) ).

In such cases it is even simpler to write

DATA text TYPE c LENGTH 255.

DATA(xstr) = cl_abap_codepage=>convert_to( source = CONV #( text ) ).

Example for influencing a calculation

IF 1 / 3 > 0.

  ...

ENDIF. 

is false, but

IF CONV decfloat34( 1 / 3 ) > 0.

  ...

ENDIF.

is true!

Example for influencing a comparison

The infamous

 

IF ' ' = ` `.

  ...

ENDIF.

is false. But

IF ' ' = CONV char1( ` ` ).

  ...

ENDIF.

is true!

Casting Operator CAST

The casting operator CAST is a constructor operator that executes an up or down cast for reference varaibles with the type specified in type.

... CAST dtype|class|interface|#( ... ) ...

  • 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.

Before release 7.40

DATA structdescr TYPE REF TO cl_abap_structdescr.
structdescr ?= cl_abap_typedescr=>describe_by_name( 'T100' ).

DATA components  TYPE abap_compdescr_tab.

components = structdescr->components.

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.

51 Comments