ARTICLE
CAST - Casting Operator
Syntax
... CAST type( 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 type
data
any object type (class or interface)
including the fully generic object type object .
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
expression CAST dtype( ... ) can be continued using a
dereferencing operator - *
; if a structured type is used, it can be specified using an
object component selector
- .
A constructor expression CAST class( ... ) can be continued with
an object component selector
- (like an object reference variable of the type class ),
and at general expression
positions and functional positions
can be continued, like an object reference variable, with an object
component selector - and used at the same operand 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 .
a specified offset/length
+off(len) can be appended (if the data
type is suitable), except if the chaining uses the statement ASSIGN
as a memory area .
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.
Example
The source code section below shows two equally valid down casts with
the two possible casting operators
?= and CAST . A feasibility of the cast is tested in a
TRY control structure before it actually takes place.
CLASS c1 DEFINITION.
ENDCLASS.
CLASS c2 DEFINITION INHERITING FROM c1.
ENDCLASS.
DATA: oref1 TYPE REF TO c1,
oref2 TYPE REF TO c2.
TRY.
CAST c2( oref1 ).
CATCH cx_sy_move_cast_error.
RETURN.
ENDTRY.
oref2 ?= oref1.
oref2 = CAST #( oref1 ).
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 ABAP Coding 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( ) ).
Documentation extract taken from SAP system, � Copyright SAP AG. All rights reserved