ABAP Keyword Documentation → ABAP − Reference → Processing Internal Data → Assignments → Assigning References → Setting Reference Variables
CAST - Casting Operator
Other versions:
7.31 | 7.40 | 7.54
Syntax
... CAST type( [let_exp] dobj ) ...
Effect
A constructor expression with the casting operator CAST
performs a
down cast or an
up cast for the argument
dobj and creates a reference variable of the static type type
as a result. type
can be specified as:
- any non-generic data type
dtype
or the fully generic data typedata
- any object type (class or interface) including the fully generic object type
object
.
- The
#
character as a symbol for the operand type. Can be specified only if the data type required in an operand position is unique and fully identifiable.
The parentheses must contain precisely one unnamed argument dobj
. If a data
type is specified, dobj
must be a data reference variable. If an object type is used, dobj
must be an object reference variable. dobj
is a
general expression position.
Casts of dobj
to the specified type are performed in accordance with the same rules as apply to the
casting operator for
assignments, ?=
. If the specified type type
is more specific than the static type of dobj
, it is a
down cast. If type
is more general than the static type of dobj
, it is an explicit
up cast.
- Like a data reference variable of type
dtype
, a constructor expressionCAST dtype( ... )
can be continued using a dereferencing operator->*
; if a structured type is used, it can be specified using an object component selector->
.
- Like an object reference variable of the type
class
, a constructor expressionCAST class( ... )
can be specified using an object component selector->
and in general expression positions and functional positions. Furthermore, it is possible to introduce standalone method calls including chained method calls.
A single expression not followed by the object component selector ->
can
be specified as a standalone statement. This makes it possible to test the feasibility of a down cast by catching the associated exception CX_SY_MOVE_CAST_ERROR.
If a single expression points to one data object or component of a data object using precisely one following object component selector, ->
,
- it can be specified as a writable expression in a
result position. The
type of the result must be convertible to the data type
type
with the following restriction: If the result is the result of a bit expression, type must be byte-like or character-like with the typec
orstring
.
- a specified
offset/length
+off(len)
can be appended (if the data type is suitable), except if the chaining uses the statementASSIGN
as a memory area.
An optional LET
expression let_exp
can be specified before the argument to define local helper fields.
Notes
- The casting operator
CAST
is suitable for avoiding the declaration of helper variables needed only for down casts.
- Explicit up casts with the casting operator
CAST
are suitable for declaring a more general type of a declared reference variable in inline declarations.
- No empty parentheses can be specified after
CAST
.
- Unlike
NEW
,CAST
works with an existing reference variable, which means that the result does not need to be saved to persist the referenced object.
- The predicate expression IS INSTANCE OF or the case distinction CASE TYPE OF can be used to check whether a down cast can be applied to specific classes or interfaces.
- Down casts are also possible using the
INTO
addition of the statementWHEN TYPE
of a case distinction usingCASE TYPE OF
.
- The constructor operator
CONV
is related toCAST
, but performs a conversion instead of a casting.
Example
The source code section below shows two equally valid down casts with the two possible
casting operators
?=
and CAST
. Before the actual cast, its feasibility
is checked using the predicate expression IS INSTANCE OF
.
CLASS c1 DEFINITION.
ENDCLASS.
CLASS c2 DEFINITION INHERITING FROM c1.
ENDCLASS.
DATA: oref1 TYPE REF TO c1,
oref2 TYPE REF TO c2.
oref1 = new c2( ).
IF oref1 IS INSTANCE OF c2.
oref2 ?= oref1.
oref2 = CAST #( oref1 ).
ENDIF.
Example
Uses dereferencing operator and component selector in CAST
with data types. The constructor expressions can be used as
writable expressions
on the right and left of assignments with the assignment operator=
.
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( ).
struc = CAST t_struc( dref )->*.
struc-col1 = CAST t_struc( dref )->col1.
struc-col2 = CAST t_struc( dref )->col2.
CAST t_struc( dref )->* = struc.
CAST t_struc( dref )->col1 = struc-col1.
CAST t_struc( dref )->col2 = struc-col2.
Example
RTTS often requires
a helper variable to perform a down cast of a type description object to a specialized class. These examples show how helper variables can be omitted by using the operator CAST
.
DATA(attributes) =
CAST cl_abap_classdescr(
cl_abap_classdescr=>describe_by_name( 'CL_ABAP_FORMAT' )
)->attributes.
DATA(components) =
CAST cl_abap_structdescr(
cl_abap_typedescr=>describe_by_name( 'T100' )
)->components.
DATA(no_of_components) =
lines( CAST cl_abap_structdescr(
cl_abap_typedescr=>describe_by_name( 'SYST' )
)->get_components( ) ).
Example
The factory method of the following class returns a reference variable of the type of the class itself.
To use interface variables to access the components declared in the interface if
,
as recommended, the variable iref
, declared inline, is cast to the type of the interface.
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( ) ).