Ansicht
Dokumentation

ABENCONSTRUCTOR_EXPRESSION_CAST - CONSTRUCTOR EXPRESSION CAST

ABENCONSTRUCTOR_EXPRESSION_CAST - CONSTRUCTOR EXPRESSION CAST

SUBST_MERGE_LIST - merge external lists to one complete list with #if... logic for R3up   CL_GUI_FRONTEND_SERVICES - Frontend Services  
This documentation is copyright by SAP AG.
SAP E-Book

CAST, Casting Operator

... CAST type( $[let_exp$] dobj ) ...

Effect

A constructor expression with the casting operator CAST performs a downcast or an upcast 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 completely generic data type data
  • any object type (class or interface) including the completely generic object type object.

The parentheses must contain exactly 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.

The casting of dobj to the specified type takes place in accordance with the same rules as for the casting operator for assignments, ?=. If the specified type type is more specific than the static type of dobj, it is a downcast. If type is more general than the static type of dobj, it is an explicit upcast.

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 downcast by catching the associated exception CX_SY_MOVE_CAST_ERROR.

If a single expression points to a data object or a component of a data object using exactly 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 restriction: If the result is the result of a bit expression, type must be byte-like or character-like with the type c or string.

An optional LET expression let_exp can be specified in front of the argument to define local helper fields.

Notes

  • The casting operator CAST is suitable for avoiding the declaration of helper variables that are only necessary for downcasts.
  • Explicit upcasts with the casting operator CAST are suitable for determining 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 cast is possible to specific classes or interfaces.
  • Downcasts are also possible using the INTO addition of the statement WHEN TYPE of a case distinction using CASE TYPE OF.

Example

The source code section below shows two equally valid downcasts with the two possible casting operators ?= and CAST. Before the actual cast, its feasibility is checked using the predicate expression IS INSTANCE OF.

Use of the dereferencing operator and component selector in CAST with data types. The constructor expressions can be used as writable expressions on the right and left side of assignments with the assignment operator=.

RTTS often requires a helper variable to perform a downcast of a type description object to a specialized class. These examples show how helper variables can be reduced by using the operator CAST.

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.






CL_GUI_FRONTEND_SERVICES - Frontend Services   CL_GUI_FRONTEND_SERVICES - Frontend Services  
This documentation is copyright by SAP AG.

Length: 8453 Date: 20240416 Time: 211930     sap01-206 ( 117 ms )