NEW - Structures
7.31 | 7.40 | 7.54
... NEW dtype|#( [let_exp]
comp1 = dobj1 comp2 = dobj2 ... ) ...
dtype is a structured data type or
for a type like this, the individual components can be specified as named arguments
comp2, ... Each component of the new anonymous data object can be assigned
a data object with the same data type as the component (or whose data type can be converted to this data type). This assignment is made for all data types in accordance with the appropriate
BASE can be specified in front of the individual component assignments, followed by a data object
dobj is a
position. The type of
dobj must be convertible to the type of the anonymous
data object. If
BASE is specified, the content of
is assigned to the anonymous data object before the individual components are assigned. If the character
# is specified for the type of the anonymous data object and the type is
not known from the operand position of the
VALUE expression, the type of
dobj is used for this expression (if known and structured).
dobj2, ... are
positions. An optional
let_exp can be specified in front of the assignments to define local helper fields that can be used on the right side of the assignments.
If a component is structured itself, either a suitable data object can be assigned to the entire substructure or its components can be specified using the structure component selector
-). Non-specified components are ignored and keep their type-specific initial
value or the value assigned using
BASE. It is not possible to assign multiple
values to a component, regardless of how the component is addressed. If the addition
BASE is used, at least one component must be specified.
- The assignments can be specified in any order within the parentheses.
- If a component with a complex data type is constructed in an argument position, the value operator
VALUEcan be used. This affects tabular components, for example. This is also possible for structured components but is not necessary since the subcomponents can be addressed using the structure component selector.
- When a constructor expression is assigned to a reference variable using
NEW, the original reference is made available in the entire expression in the target variable. The target variable is not overwritten until the expression is closed. In the case of the value operator
VALUE, however, the target variable can only be assigned to a helper variable using
LETand is then no longer available.
Constructs an anonymous data object with a nested structure type and tabular components. The subcomponents
col2 are addressed directly using the structure component selector.
VALUE must be used to construct the tabular component
col3. This is because the
syntax used to construct internal tables cannot be specified directly as an argument.
TYPES: t_itab TYPE TABLE OF i WITH EMPTY KEY, BEGIN OF t_struct, col1 TYPE i, BEGIN OF col2, col1 TYPE i, col2 TYPE t_itab, END OF col2, col3 TYPE t_itab, END OF t_struct. DATA itab TYPE t_itab. DATA dref TYPE REF TO data. dref = NEW t_struct( col1 = 1 col2-col1 = 2 col2-col2 = itab col3 = VALUE #( ( 1 ) ( 2 ) ( 3 ) ) ).
BASE. The type of the return value of
is applied in the construction of
ref1. This is not possible in the construction
base2 is not structured. In both
col3 have the values xx
or zz assigned using
col2 has the directly assigned value BB.
TYPES: BEGIN OF struct, col1 TYPE c LENGTH 2, col2 TYPE c LENGTH 2, col3 TYPE c LENGTH 2, END OF struct. DATA(base1) = VALUE struct( col1 = 'xx' col2 = 'yy' col3 = 'zz' ). DATA(ref1) = NEW #( BASE base1 col2 = 'BB' ). DATA(base2) = `xxyyzz`. DATA(ref2) = NEW struct( BASE base2 col2 = 'BB' ). cl_demo_output=>write( ref1->* ). cl_demo_output=>display( ref2->* ).
See also the examples for the value operator