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.
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.
Method cl_abap_codepage=>convert_to expects a string but you want to convert a text field.
DATA text TYPE c LENGTH 255.
DATA helper TYPE string.
DATA xstr TYPE xstring.
helper = text.
xstr = cl_abap_codepage=>convert_to( source = helper ).
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 ) ).
IF 1 / 3 > 0.
...
ENDIF.
is false, but
IF CONV decfloat34( 1 / 3 ) > 0.
...
ENDIF.
is true!
The infamous
IF ' ' = ` `.
...
ENDIF.
is false. But
IF ' ' = CONV char1( ` ` ).
...
ENDIF.
is true!
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 can write a compnent selector -> directly behind CAST type( ... ).
Common example where a down cast is needed.
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.
DATA(components) = CAST cl_abap_structdescr(
cl_abap_typedescr=>describe_by_name( 'T100' ) )->components.
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( ) ).
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
5 | |
3 | |
3 | |
2 | |
2 | |
2 | |
1 | |
1 | |
1 | |
1 |