louis-lee_abap-programing.bw
louis-lee_abap-programing.bw
Type Conversions, Compatibility and Type Conversions, Compatibility and Creating Structured Types
Assignments (cont) Assignments (cont)
The type name is preceded by BEGIN
The operands of many ABAP statements The applies_to_data method of the RTTI OF (which marks the beginning of the
structured type definition) and END OF (the
are assigned internally according to the class cl_abap_datadescr can be used to
end of the definition).
assignment rules. check type compatibility.
The components - at least one must be
defined - are listed in between.
Typically, assignements are made using the Non-Admissible Values of Literals
Such structured type definitions are
assignment operator =. If necessary and Note recent syntax warnings when using usually grouped together in a chained
applicable, the type is converted implicitly. literals that represent invalid values for statement, i.e. TYPES is followed by a
target types. The following example colon, and the components are separated
However, you can also use the conversion
demonstrates the assignment of literals by commas.
operator CONV to convert types explicitly.
using admissible and non-admissible The simplest structures and structured
values. You can copy and paste the code types have elementary components.
For lossless assignments, the lossless
into a demo class in your SAP BTP ABAP As mentioned above, the components
operator EXACT can be used to perform Environment to explore the syntax can be of any type, i.e. they can be of
checks before the conversion is performed warnings. structured types themselves, internal table
to ensure that only valid values are DATA char3 TYPE c LENGTH 3. types, or reference types.
assigned and that no values are lost in You can use the TYPE and LIKE
"Value is admissable and convertible additions for the types of the components.
assignments.
char3 = 'abc'. You can use the LINE OF addition to refer
to a table type or an internal table.
In general, no checks are performed on
"Non-admissable value assigned to the
assignments between compatible data Outside of classes, you can also refer to
target (type c length 6)
objects. If a data object already contains an DDIC types using LIKE (... comp11 LIKE
char3 = 'defghi'.
invalid value, for example, an invalid date or ddic_type, ...). If you actually want to refer to
time in a date or time field, it is passed a DATA date TYPE d. an existing data object, but due to typing
valid value when the assignment is made to errors you inadvertently specify a name that
"Value is admissable and convertible exists as DDIC type, errors may be unavoi‐
a compatible data object.
date = '20250101'.
dable.
Demo Example Data Type And Data Object Populating Structures (cont) Including Structures
Including Structures (cont) Elementary data types Creating Types/Data Objects at Runtime
By using the optional AS addition and Elementary (or scalar) data types are based Using Runtime Type Services (RTTS) you
directly on a set of built-in ABAP types. can ...
specifying a name, the included
Numeric types: Integers (b, s, i, int8), get type information on data objects, data
components can be addressed by this
decimal floating point numbers (decfloat16, types or instances at runtime (Runtime
common name as if they were actually
decfloat34), binary floating point numbers Type Identification (RTTI)).
components of a substructure. (f), and packed numbers (p) define and create new data types as type
The optional RENAMING WITH SUFFIX Character-like types: text fields (c) and description objects at runtime (Runtime
addition, followed by a name, gives the numeric text fields (n) Type Creation (RTTC)).
Byte-like type: byte fields (x)
included components a suffix name to avoid
Character-like date and time types: date ABAP Enumerated Types and Objects
naming conflicts with other components.
fields (d) and time fields (t)
ABAP supports the concept of enumer‐
Time stamp type for time stamp fields
Demo ABAP Structure ations.
(utclong).
Enumerations are a mixture of types and
Executable Example Character-like type for text strings (string)
constants.
Byte-like type for byte strings (xstring)
An enumerated type specifies a value set
Generic Types
The data types c, n, x, and p are incomp‐ in addition to the actual type properties.
Generic types are available with which Enumerated objects - data objects with
lete, i.e., generic data types, with respect to
formal parameters of methods or field an enumerated type - are mainly used to
their length. The type definition syntax has
symbols can be specified. check allowed values. This usually restricts
At runtime, the actual data type is copied a special addition for this (LENGTH). In
the actual parameters passed to methods to
from the assigned actual parameter or addition, p is also generic with respect to the enumerated values defined in the class.
memory area, i.e. they receive the complete the number of decimal places (DECIMALS Enumerated variables are variable
data type only when an actual parameter is enumerated objects. They can only contain
addition). See more about generic types in
passed or a memory area is assigned. the associated enumerated values.
the following sections.
CDS enumerated types are also
The TYPE REF TO addition types as a
available.
reference variable. A generic type cannot be
specified after REF TO. A typing with TYPE
REF TO data and TYPE REF TO object is
considered as completely typing.
To create a structure (i.e. a structured data are used to combine different data comp2 TYPE c LENGTH 15,
object) in an ABAP program, you can use objects that belong together. A typical comp3 TYPE p LENGTH 8 DECIMALS 2,
the DATA keyword. example is an address. It has several ...,
It works in the same way as the TYPES components, such as name, street, city, END OF struc.
statement above. and so on, that belong together. Nested structures: At least one
Unlike the TYPES statement, you can use play an important role in the context of component of a structure is a substructure,
the VALUE addition to set default values. internal tables and database tables. that is, it refers to another structure. The
Structured types serve as line types for following example has multiple substruct‐
The keywords CLASS-DATA and
these tables. Most internal tables across ures.
CONSTANTS can also be used to create
ABAP programs may have structured line DATA: BEGIN OF address_n,
structures. In principle, they represent types. For database tables, there is no BEGIN OF name,
special cases of the general statement alternative to structured line types. title TYPE string VALUE Mr.,
shown above. can be created locally in an ABAP prename TYPE string VALUE Duncan,
program and globally. surname TYPE string VALUE Pea,
Structures can also be created inline using
END OF name,
DATA(...) or FINAL(...).
Variants of Structures BEGIN OF street,
Flat structures contain only elementary name TYPE string VALUE Vegetable La
Structures
types that have a fixed length, that is, there ne,
Structures ...
are no internal tables, reference types or num TYPE string VALUE 11,
are data objects with structured data
strings as components. Nesting does not END OF street,
types (which is a complex data type
matter in this context. Even a nested BEGIN OF city,
because it is composed of other data
structure is considered flat unless a substr‐ zipcode TYPE string VALUE 349875,
types).
ucture contains a deep component. name TYPE string VALUE Botanica,
consist of a sequence of components of
DATA: BEGIN OF struc,
any data type, that is, the components of a
comp1 TYPE i,
structure can be, for example, elementary
data objects, structures themselves, internal
tables or references.
END OF city, The sy (or syst) structure is a built-in data You can reset individual components to
END OF address_n. object. their initial values and clear the entire
Deep structures: Contains at least one The components of the structure represent structure using the CLEAR keyword. Note
internal table, reference type, or string as a ABAP system fields. that FREE statements also deletes the
component. These fields, filled by the ABAP runtime content, but they also release the initially
DATA: BEGIN OF address_d, framework, can be used to query system allocated memory. space.
name TYPE string VALUE Mr. Duncan information and more.
Pea, Typically, they should only be read, and not Complex Data Types
street TYPE string VALUE Vegetable La overwritten.
Are composed of other types.
Prominent system fields are the following
ne 11, Structured types: Represent a sequence of
city TYPE string VALUE 349875 Botani sy-subrc: Return code of many ABAP
arbitrary data types (i.e., they can be
statements; typically, the value 0 indicates
ca, elementary, reference, or complex data
success
details TYPE TABLE OF some_table WITH types). The typical syntax element for the
sy-tabix: Row index of internal tables
EMPTY KEY, local definition of a structure is ... BEGIN
sy-index: Loop pass index
END OF address_d. OF ... END OF ....
These ones and others can be used in
Table types: Consist of a sequence of any
The data types of DDIC types are all flat ABAP for Cloud Development. However,
number of lines of the same data type. It
(not nested) structures. Exception: most of the fields should not be used in
can be any elementary type, reference type,
Components of type string can be ABAP for Cloud Development (indicated by
or complex data type. The type definition
a syntax warning) because they refer to
contained. includes other properties such as the table
Standard ABAP contexts (e.g. classic
Work areas of ABAP SQL statements category (defines how tables can be
dynpros and lists), or their values are not
accessed) and table key (to identify the
cannot contain any deep components other
relevant in a cloud context.
table lines). The typical syntax element is ...
than strings among others.
TABLE OF ....
Especially for assignments and compar‐
isons of deep structures, the compatibility of
the source and target structure must be
taken into account.
Complex Data Types (cont) Data objects (cont) Typed Literals in ABAP SQL (cont)
Enumerated types: Specify a set of values The type of data that a data object can Advantages of typed literals over untyped
in addition to the actual type properties. The receive is determined by its data type. literals
typical syntax element is ... BEGIN OF Like data types, their existence and Allow type-safe use of literals
ENUM ... END OF ENUM .... visibility depend on the declaration context. Eliminate the need for (implicit type)
Mesh types: Special structured type that Are usually used in ABAP statements by conversions and casts, which can lead to
contains only table types with structured line specifying them in the operand position. surprising or erroneous results; also
types as components that can be linked consider the conversion costs in terms of
using mesh associations. The typical syntax Ranges Tables performance (typed literals are passed to
element is ... BEGIN OF MESH ... END OF the database and evaluated there without
Internal tables that have the predefined
MESH .... ABAP-specific type conversions).
columns SIGN, OPTION, LOW, and HIGH
BDEF derived types: RAP-specific For better readability (you can immedi‐
Declared with the TYPE RANGE OF
structured and table types. The typical ately see what type is being used)
addition in DATA and TYPES statements
syntax elements are ... TYPE STRUCTURE
Used to store range conditions that can
FOR ... and ... TYPE TABLE FOR .... More Globally Available Structures and
be evaluated in expressions using the IN
information can be found here and in the Structured Types
operator (each row in the table represents a
ABAP cheat sheet on ABAP EML.
separate comparison) Apart from the local declaration of a
structured type, you can create such a type,
Data objects
Typed Literals in ABAP SQL for example, as global DDIC structure in the
Are objects (or instances) of a data type ABAP Dictionary. Such a DDIC structure
Literal whose data types is defined by
(similar to objects/instances of classes in defines a globally available structured type
specifying a built-in dictionary type explic‐
ABAP Objects). (DDIC type).
itly.
Occupy memory space and exist in
Available for most but not all ABAP
different forms, for example, numeric or There are other structured types
Dictionary data types.
textual data can be contained in data available globally, which may be the
Can be used in ABAP SQL and in ABAP
objects. structured types most commonly used in
CDS.
ABAP programs:
Globally Available Structures and Creating Structures by Inline Declaration Accessing (Components of) Structures
Structured Types (cont) (cont)
This is particularly useful for declaring
This means that when you create a data objects at the operand positions where For variables with reference to a
structure in your ABAP program, for you actually need them. structured data object, the object
example, you can simply use the name of a In this way, you can avoid an extra component selector -> can be used: ...dref-
database table to address the line type of declaration of the structure in different >comp .... The following syntax also works,
the table. The structure you created will contexts. but is less convenient: ... dref->*-comp ....
then have the same structured type as the You can use the declaration operator ADT and the ABAP Editor provide code
database table. Typically, you use the using DATA(...). The FINAL declaration completion for structure components after
database tables to create structures of such operator is used to create immutable the component selectors.
a type, or internal tables of such a variables.
structured line type, to process data read You can also create structures using the Processing Structures
from the database table in structures or VALUE operator (and also fill them as
Structures in ABAP SQL Statements
internal tables. shown below). Without specifying
Structures in Statements for Processing
. Various CDS entities are globally available component values in the parentheses, you
Internal Tables
structured types. For example, a CDS view create an initial structure.
entity represents a structured data type and
Boxed Components
can be used as such in ABAP programs Accessing (Components of) Structures
In structures, boxed components represent
(but not in the ABAP Dictionary).
Structures can be accessed as a whole.
nested structures managed by an internal
You can also address the individual
reference.
Structures and structured data types can be
components of structures at the appropriate
Currently, static boxes are supported as
defined in the public visibility section of
operand positions.
boxed components, enabling initial value
global classes or in global interfaces and
To address the components, use the
sharing. Find more information here.
then used globally.
structure component selector -.
When used: