ABAP Keyword Documentation → ABAP − Reference → Creating Objects and Values → NEW - Instance Operator
NEW - Structures
Other versions:
7.31 | 7.40 | 7.54
Syntax
... NEW dtype|#( [let_exp]
[BASE dobj]
comp1 = dobj1 comp2 = dobj2 ... ) ...
Effect
If dtype
is a structured data type or #
stands
for a type like this, the individual components can be specified as named arguments comp1
,
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
assignment rules.
An addition BASE
can be specified in front of the individual component assignments, followed by a data object dobj
. dobj
is a
functional operand
position. The type of dobj
must be convertible to the type of the anonymous
data object. If BASE
is specified, the content of dobj
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).
dobj1
, dobj2
, ... are
general expression
positions. An optional LET
expression
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.
Notes
- 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
VALUE
can 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 operatorVALUE
, however, the target variable can only be assigned to a helper variable usingLET
and is then no longer available.
Example
Constructs an anonymous data object with a nested structure type and tabular components. The subcomponents
of 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 ) ) ).
Example
Uses BASE
. The type of the return value of base1
is applied in the construction of ref1
. This is not possible in the construction
of ref2
, since base2
is not structured. In both
results, col1
and col3
have the values xx
or zz assigned using BASE
, whereas 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->* ).
Example
See also the examples for the value operator VALUE
.