Get Example source ABAP code based on a different SAP table
• ?= ABAP_DOWN_CAST • = ABAP_UP_CAST
=, ?=, Upcast and Downcast
ABAP_SYNTAX destination_ref =$|?= source_ref.>
What does it do? Assignment between two reference variables. The reference in source_ref> is assigned destination_ref>. After a successful assignment, destination_ref> points to the same object as source_ref> (reference semantics>). The assignment of reference variables is a special form of assignments of data objects>, whereby two assignment operators are available for assignments between reference variables and are used in accordance with the assignment rules for reference variables >:
In assignments between reference variables, the general assignment operator =>> can only be used for upcasts> in which the static type of source_ref> is more specific than or the same as the static type of destination_ref>.
The special casting operator> ?= > can only be used for assignments between reference variables. If the static type of source_ref> is more general than the static type of destination_ref>, ?=> must be used to produce a downcast>. If this is known statically, it is checked by the syntax check, otherwise it is checked at runtime. The actual downcast, that is, the check to see whether assignments are possible in accordance with the assignment rules> for reference variables, only takes place at runtime. Then, if the static type of destination_ref> is not more general or is the same as the dynamic type of source_ref>, a catchable exception is raised and the target variable keeps its original value. The same applies to the right side and left side as when assigning data objects>, with the following restrictions:
The data types of the source and target are reference types>, which means that functional method calls>, and constructor expressions> and table expressions> can be specified on the right side whose return value has a reference type. Built-in functions> and calculation expressions> do not return any reference variables and are not possible here.
An inline declaration DATA(var)>> or FINAL(var)>> is possible only on the left side of =>, and not on the left side of ?=>. The static types of the reference variable source_ref > is used, which must be known statically.
Latest notes:
The casting operator ?=> can always be specified, even for upcasts. This is, however, not usually necessary.
If it is possible to know statically that an assignment is not possible, neither => nor ?=> can be used. This is the case, for example, when the static types of source variables and target variables are classes from different paths of the inheritance tree.
The null reference> of an initial reference variable can be assigned to every target variable in a downcast that can be specified here. The same applies to a non-initial invalid reference that no longer points to an object.
For non-initial reference variables, the predicate expression IS INSTANCE OF>> or the case distinction CASE TYPE OF>> can be used to check whether a downcast is possible on specific classes or interfaces.
In addition to ?=>, the casting operator> CAST>> also enables downcasts in operand positions, which helps to reduce helper variables.
Downcasts are also possible using the INTO> addition of the statement WHEN TYPE>> of a case distinction using CASE TYPE OF>>.
The casting operator ?=> cannot be used in multiple assignments>. NON_V5_HINTS
An obsolete form of downcast is the statement MOVE>> with the addition ?TO>>. ABAP_HINT_END
ABAP_EXAMPLE_VX5 The first two assignments in the following source code section are upcasts:
The instance operator NEW> creates a result with the static and dynamic type c2>, which can be assigned to the more general reference variable oref1>.
Any reference variable can be assigned to the reference variable oref> with the most general static type object>. The next two assignments are downcasts:
It is only possible to check at runtime whether the general reference variable oref> points to an object that can also point to oref2>. This is the case in the example.
The downcast of oref2> to oref3>, however, fails at runtime and raises the caught exception. ABEXA 00446 ABAP_EXAMPLE_END
Runtime Exceptions
Catchable Exceptions CX_SY_MOVE_CAST_ERROR>>
Reason for error:
Type conflict in downcast
Runtime error:
MOVE_CAST_ERROR>
Reason for error:
Source variable or target variable is not a reference variable