Update
This commit is contained in:
@@ -15,6 +15,8 @@
|
||||
- [Methods](#methods)
|
||||
- [Parameter Interface](#parameter-interface)
|
||||
- [Formal and Actual Parameters](#formal-and-actual-parameters)
|
||||
- [Complete Typing of Formal Parameters](#complete-typing-of-formal-parameters)
|
||||
- [Generic Typing of Formal Parameters](#generic-typing-of-formal-parameters)
|
||||
- [Defining Parameters as Optional](#defining-parameters-as-optional)
|
||||
- [Defining Input Parameters as Preferred](#defining-input-parameters-as-preferred)
|
||||
- [Constructors](#constructors)
|
||||
@@ -559,6 +561,386 @@ In the simplest form, methods can have no parameter at all. Apart from that, met
|
||||
- If passing by reference is used, a local data object is not created for the actual parameter. Instead, the procedure is given a [reference](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenreference_glosry.htm "Glossary Entry") to the actual parameter during the call and works with the actual parameter itself.
|
||||
- Note that parameters that are input and passed by reference cannot be modified in the procedure. However, the use of a reference is beneficial regarding the performance compared to creating a local data object.
|
||||
|
||||
The following example shows a class with a simple method demonstrating the syntax for formal paremeter specifications. Complete types are used.
|
||||
|
||||
```abap
|
||||
CLASS zcl_some_class DEFINITION
|
||||
PUBLIC
|
||||
FINAL
|
||||
CREATE PUBLIC .
|
||||
|
||||
PUBLIC SECTION.
|
||||
INTERFACES if_oo_adt_classrun.
|
||||
|
||||
"Passing by reference and value
|
||||
METHODS: meth IMPORTING i_a TYPE i
|
||||
REFERENCE(i_b) TYPE string
|
||||
VALUE(i_c) TYPE i
|
||||
RETURNING VALUE(r_a) TYPE string.
|
||||
|
||||
|
||||
PROTECTED SECTION.
|
||||
PRIVATE SECTION.
|
||||
ENDCLASS.
|
||||
|
||||
|
||||
|
||||
CLASS zcl_some_class IMPLEMENTATION.
|
||||
|
||||
METHOD if_oo_adt_classrun~main.
|
||||
|
||||
DATA(result) = meth( i_a = 1
|
||||
i_b = `hello`
|
||||
i_c = 2 ).
|
||||
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD meth.
|
||||
... "Method implementation
|
||||
|
||||
"No change for input parameters passed by reference
|
||||
"i_a += 1.
|
||||
"i_b &&= ` world`.
|
||||
|
||||
"Input parameters passed by reference can be changed in the method.
|
||||
i_c += 1.
|
||||
|
||||
ENDMETHOD.
|
||||
|
||||
ENDCLASS.
|
||||
```
|
||||
|
||||
<p align="right"><a href="#top">⬆️ back to top</a></p>
|
||||
|
||||
#### Complete Typing of Formal Parameters
|
||||
|
||||
Syntax for [completely](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abencomplete_data_type_glosry.htm "Glossary Entry") typing a formal parameter:
|
||||
- `TYPE complete_type`
|
||||
- `TYPE LINE OF complete_type`
|
||||
- `TYPE REF TO type`
|
||||
- `LIKE dobj`
|
||||
- `LIKE LINE OF dobj`
|
||||
- `LIKE REF TO dobj`
|
||||
|
||||
> **💡 Note**<br>
|
||||
> - `complete_type`: Stands for a non-generic built-in ABAP, ABAP DDIC, ABAP CDS, a public data type from a global class or interface, or a local type declared with `TYPES`
|
||||
> - `REF TO` types as a 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 a formal parameter.
|
||||
> - Enumerated types can also be used to type the formal parameter.
|
||||
> - The considerations also apply to the typing of field symbols.
|
||||
|
||||
```abap
|
||||
CLASS zcl_some_class DEFINITION
|
||||
PUBLIC
|
||||
FINAL
|
||||
CREATE PUBLIC .
|
||||
|
||||
PUBLIC SECTION.
|
||||
INTERFACES if_oo_adt_classrun.
|
||||
|
||||
"Local types and data objects used in the example
|
||||
TYPES c3 TYPE c LENGTH 3.
|
||||
TYPES der_type TYPE TABLE FOR CREATE zdemo_abap_rap_ro_m.
|
||||
DATA int TYPE i.
|
||||
DATA itab TYPE TABLE OF zdemo_abap_fli_ve WITH EMPTY KEY.
|
||||
|
||||
"Various syntax options for completely typing formal parameters
|
||||
"Note: The example parameters are all specified for passing
|
||||
"actual parameters by reference.
|
||||
METHODS: meth IMPORTING
|
||||
"---- Non-generic built-in ABAP types ----
|
||||
i_a TYPE i
|
||||
i_b TYPE string
|
||||
"---- ABAP DDIC types ----
|
||||
i_c TYPE land1 "elementary type
|
||||
i_d TYPE timestampl "elementary type
|
||||
i_e TYPE zdemo_abap_fli "structured type based on DDIC database table
|
||||
i_f TYPE string_hashed_table "table type
|
||||
"---- ABAP CDS types (all of the examples are structured types) ----
|
||||
i_g TYPE zdemo_abap_fli_ve "CDS view entity
|
||||
i_h TYPE zdemo_abap_abstract_ent "CDS abstract entity
|
||||
i_i TYPE zdemo_abap_table_function "CDS table function
|
||||
"---- Data types declared in public section of a class ----
|
||||
i_j TYPE zcl_demo_abap_dtype_dobj=>t_pub_text_c30 "elementary type
|
||||
i_k TYPE zcl_demo_abap_amdp=>carr_fli_struc "structured type
|
||||
i_l TYPE zcl_demo_abap_amdp=>carr_fli_tab "table type
|
||||
"---- Data types declared in an interface ----
|
||||
i_m TYPE zdemo_abap_get_data_itf=>occ_rate "elementary type
|
||||
i_n TYPE zdemo_abap_get_data_itf=>carr_tab "table type
|
||||
"---- Local types ----
|
||||
i_o TYPE c3 "elementary type
|
||||
i_p TYPE der_type "table type (BDEF derived type)
|
||||
"---- Note: Examaples for not allowed types of formal parameters ----
|
||||
"i_no1 TYPE c LENGTH 3
|
||||
"i_no2 TYPE TABLE OF zdemo_abap_fli WITH EMPTY KEY
|
||||
"---- Reference types ----
|
||||
i_q TYPE REF TO i "Data reference
|
||||
i_r TYPE REF TO zdemo_abap_carr "Data reference
|
||||
i_s TYPE REF TO zcl_demo_abap_unit_test "Object reference
|
||||
i_t TYPE REF TO data "Data reference (considered as complete typing, too)
|
||||
i_u TYPE REF TO object "Object reference (considered as complete typing, too)
|
||||
"---- TYPE LINE OF addition (structured type based on a table type) ----
|
||||
i_v TYPE LINE OF zcl_demo_abap_amdp=>carr_fli_tab
|
||||
i_w TYPE LINE OF der_type
|
||||
"---- LIKE addition (types based on existing data objects) ----
|
||||
i_x LIKE int "Local type
|
||||
i_y LIKE zcl_demo_abap_dtype_dobj=>comma "Constant specified in a class
|
||||
i_z LIKE zdemo_abap_objects_interface=>stat_str "Data object specified in an interface
|
||||
"---- LIKE LINE OF addition (types based on existing internal tables) ----
|
||||
i_1 LIKE LINE OF itab "Local type
|
||||
"---- LIKE REF TO addition (reference types based on existing data object) ----
|
||||
i_2 LIKE REF TO int "Local type (elementary)
|
||||
i_3 LIKE REF TO itab "Local type (table)
|
||||
.
|
||||
|
||||
PROTECTED SECTION.
|
||||
PRIVATE SECTION.
|
||||
ENDCLASS.
|
||||
|
||||
|
||||
|
||||
CLASS zcl_some_class IMPLEMENTATION.
|
||||
|
||||
METHOD if_oo_adt_classrun~main.
|
||||
|
||||
"Calling methods and providing actual parameters
|
||||
* meth(
|
||||
* i_a = 1
|
||||
* i_b = `hello`
|
||||
* ... ).
|
||||
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD meth.
|
||||
... "Method implementation
|
||||
ENDMETHOD.
|
||||
|
||||
ENDCLASS.
|
||||
```
|
||||
|
||||
<p align="right"><a href="#top">⬆️ back to top</a></p>
|
||||
|
||||
#### Generic Typing of Formal Parameters
|
||||
|
||||
Find more information on generic types in the [Data Types and Data Objects](16_Data_Types_and_Objects.md#generic-types) cheat sheet.
|
||||
|
||||
```abap
|
||||
CLASS zcl_some_class DEFINITION
|
||||
PUBLIC
|
||||
FINAL
|
||||
CREATE PUBLIC .
|
||||
|
||||
PUBLIC SECTION.
|
||||
INTERFACES if_oo_adt_classrun.
|
||||
|
||||
"Example method demonstrating the generic typing of formal parameters
|
||||
METHODS: meth IMPORTING
|
||||
"---- Any data type ----
|
||||
i_data TYPE data
|
||||
i_any TYPE any
|
||||
|
||||
"---- Character-like types ----
|
||||
i_c TYPE c "Text field with a generic length
|
||||
i_clike TYPE clike "Character-like (c n string d t and character-like flat structures)
|
||||
i_csequence TYPE csequence "Text-like (c string)
|
||||
i_n TYPE n "Numeric text with generic length
|
||||
i_x TYPE x "Byte field with generic length
|
||||
i_xsequence TYPE xsequence "Byte-like (x xstring)
|
||||
|
||||
"---- Numeric types ----
|
||||
i_decfloat TYPE decfloat "decfloat16 decfloat34
|
||||
i_numeric TYPE numeric "Numeric ((b s) i int8 p decfloat16 decfloat34 f)
|
||||
i_p TYPE p "Packed number (generic length and number of decimal places)
|
||||
|
||||
"---- Internal table types ----
|
||||
i_any_table TYPE ANY TABLE "Internal table with any table type
|
||||
i_hashed_table TYPE HASHED TABLE
|
||||
i_index_table TYPE INDEX TABLE
|
||||
i_sorted_table TYPE SORTED TABLE
|
||||
i_standard_table TYPE STANDARD TABLE
|
||||
i_table TYPE table "Standard table
|
||||
|
||||
"---- Other types ----
|
||||
i_simple TYPE simple "Elementary data type including enumerated types and
|
||||
"structured types with exclusively character-like flat components
|
||||
.
|
||||
PROTECTED SECTION.
|
||||
PRIVATE SECTION.
|
||||
ENDCLASS.
|
||||
|
||||
|
||||
|
||||
CLASS zcl_some_class IMPLEMENTATION.
|
||||
|
||||
METHOD if_oo_adt_classrun~main.
|
||||
|
||||
"Structure including various components of specific types
|
||||
"They represent actual parameters in the method call below
|
||||
DATA: BEGIN OF s,
|
||||
c3 TYPE c LENGTH 3,
|
||||
c10 TYPE c LENGTH 10,
|
||||
n4 TYPE n LENGTH 4,
|
||||
str TYPE string,
|
||||
time TYPE t,
|
||||
date TYPE d,
|
||||
dec16 TYPE decfloat16,
|
||||
dec34 TYPE decfloat34,
|
||||
int TYPE i,
|
||||
pl4d2 TYPE p LENGTH 4 DECIMALS 2,
|
||||
tab_std TYPE STANDARD TABLE OF string WITH EMPTY KEY,
|
||||
tab_so TYPE SORTED TABLE OF string WITH NON-UNIQUE KEY table_line,
|
||||
tab_ha TYPE HASHED TABLE OF string WITH UNIQUE KEY table_line,
|
||||
xl1 TYPE x LENGTH 1,
|
||||
xstr TYPE xstring,
|
||||
structure TYPE zdemo_abap_carr, "character-like flat structure
|
||||
END OF s.
|
||||
|
||||
"The following method call specifies various actual parameters for the
|
||||
"generic formal parameters.
|
||||
"Note the comments for allowed and not allowed example assignments of
|
||||
"actual parameters.
|
||||
meth(
|
||||
"------------- Any data type -------------
|
||||
"--- data/any: Allowed (examples) ---
|
||||
i_data = s-c3
|
||||
"i_data = s-time
|
||||
"i_data = s-tab_std
|
||||
"i_data = s-xstr
|
||||
|
||||
i_any = s-c3
|
||||
"i_any = s-time
|
||||
"i_any = s-tab_std
|
||||
"i_any = s-xstr
|
||||
|
||||
"------------- Character-like types -------------
|
||||
"--- c: Allowed (examples) ---
|
||||
i_c = s-c3
|
||||
"i_c = s-c10
|
||||
"--- c: Not allowed (examples) ---
|
||||
"i_c = s-str
|
||||
"i_c = s-n4
|
||||
|
||||
"--- clike: Allowed (examples) ---
|
||||
i_clike = s-c3
|
||||
"i_clike = s-c10
|
||||
"i_clike = s-str
|
||||
"i_clike = s-structure
|
||||
"i_clike = s-time
|
||||
"i_clike = s-date
|
||||
"i_clike = s-n4
|
||||
"--- clike: Not allowed (examples) ---
|
||||
"i_clike = s-xstr
|
||||
"i_clike = s-xl1
|
||||
"i_clike = s-pl4d2
|
||||
|
||||
"--- csequence: Allowed (examples) ---
|
||||
i_csequence = s-c3
|
||||
"i_csequence = s-c10
|
||||
"i_csequence = s-str
|
||||
"--- csequence: Not allowed (examples) ---
|
||||
"i_csequence = s-time
|
||||
"i_csequence = s-date
|
||||
"i_csequence = s-structure
|
||||
|
||||
"--- n: Allowed ---
|
||||
i_n = s-n4
|
||||
"--- n: Not allowed (examples) ---
|
||||
"i_n = s-c3
|
||||
"i_n = s-int
|
||||
|
||||
"--- x: Allowed ---
|
||||
i_x = s-xl1
|
||||
"--- x: Not allowed (examples) ---
|
||||
"i_x = s-xstr
|
||||
"i_x = s-c3
|
||||
|
||||
"--- xsequence: Allowed ---
|
||||
i_xsequence = s-xstr
|
||||
"i_xsequence = s-xl1
|
||||
"--- xsequence: Not allowed (examples) ---
|
||||
"i_xsequence = s-c3
|
||||
"i_xsequence = s-str
|
||||
|
||||
"--- decfloat: Allowed ---
|
||||
i_decfloat = s-dec16
|
||||
"i_decfloat = s-dec34
|
||||
"--- decfloat: Not allowed (examples) ---
|
||||
"i_decfloat = s-int
|
||||
"i_decfloat = s-pl4d2
|
||||
|
||||
"--- numeric: Allowed (examples) ---
|
||||
i_numeric = s-int
|
||||
"i_numeric = s-dec16
|
||||
"i_numeric = s-dec34
|
||||
"i_numeric = s-pl4d2
|
||||
"--- numeric: Not allowed (examples) ---
|
||||
"i_numeric = s-n4
|
||||
"i_numeric = s-date
|
||||
|
||||
"--- p: Allowed ---
|
||||
i_p = s-pl4d2
|
||||
"--- p: Not allowed (examples) ---
|
||||
"i_p = s-dec16
|
||||
"i_p = s-dec34
|
||||
|
||||
"--- any table: Allowed ---
|
||||
i_any_table = s-tab_std
|
||||
"i_any_table = s-tab_ha
|
||||
"i_any_table = s-tab_so
|
||||
"--- any table: Not allowed (examples) ---
|
||||
"i_any_table = s-structure
|
||||
"i_any_table = s-c3
|
||||
|
||||
"--- hashed table: Allowed ---
|
||||
i_hashed_table = s-tab_ha
|
||||
"--- hashed table: Not allowed ---
|
||||
"i_hashed_table = s-tab_std
|
||||
"i_hashed_table = s-tab_so
|
||||
|
||||
"--- index table: Allowed ---
|
||||
i_index_table = s-tab_std
|
||||
"i_index_table = s-tab_so
|
||||
"--- index table: Not allowed ---
|
||||
"i_index_table = s-tab_ha
|
||||
|
||||
"--- sorted table: Allowed ---
|
||||
i_sorted_table = s-tab_so
|
||||
"--- index table: Not allowed ---
|
||||
"i_sorted_table = s-tab_std
|
||||
"i_sorted_table = s-tab_ha
|
||||
|
||||
"--- standard table/table: Allowed ---
|
||||
i_standard_table = s-tab_std
|
||||
i_table = s-tab_std
|
||||
"--- standard table/table: Not allowed ---
|
||||
"i_standard_table = s-tab_so
|
||||
"i_standard_table = s-tab_ha
|
||||
"i_table = s-tab_so
|
||||
"i_table = s-tab_ha
|
||||
|
||||
"--- simple: Allowed (examples) ---
|
||||
i_simple = s-structure
|
||||
"i_simple = s-c3
|
||||
"i_simple = s-n4
|
||||
"i_simple = s-int
|
||||
"i_simple = s-pl4d2
|
||||
"i_simple = s-xstr
|
||||
"i_simple = s-str
|
||||
"--- simple: Not allowed (examples) ---
|
||||
"i_simple = s-tab_ha
|
||||
"i_simple = s-tab_so
|
||||
|
||||
).
|
||||
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD meth.
|
||||
... "Method implementation
|
||||
ENDMETHOD.
|
||||
|
||||
ENDCLASS.
|
||||
```
|
||||
|
||||
|
||||
<p align="right"><a href="#top">⬆️ back to top</a></p>
|
||||
|
||||
#### Defining Parameters as Optional
|
||||
|
||||
@@ -1490,7 +1490,7 @@ ENDDO.
|
||||
- Two flavors for iterations using [`FOR`](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenfor.htm):
|
||||
- [Conditional iterations](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenfor_conditional.htm)
|
||||
(including the ABAP words `UNTIL` and `WHILE` which
|
||||
have the semantics of ABAP statements
|
||||
have the semantics of the ABAP statements
|
||||
[`DO`](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abapdo.htm)
|
||||
and
|
||||
[`WHILE`](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abapwhile.htm))
|
||||
@@ -1660,23 +1660,28 @@ ENDLOOP.
|
||||
"The objective of the following example is to extract the content of the segments that
|
||||
"are positioned within /.../ in a URL. The segments are stored in an internal table.
|
||||
DATA(url) = `https://help.sap.com/docs/abap-cloud/abap-concepts/controlled-sap-luw/`.
|
||||
FIND ALL OCCURRENCES OF PCRE `(?<=/)([^/]+)(?=/)` IN url RESULTS DATA(res).
|
||||
FIND ALL OCCURRENCES OF PCRE `(?<=\/)([^\/]+)(?=\/)` IN url RESULTS DATA(res).
|
||||
|
||||
"Details on the regular expression:
|
||||
"- Positive lookbehind (?<=/) that determines that the content is preceded by `/`
|
||||
"- Positive lookahead (?=/) that determines that the content is followed by `/
|
||||
"- ([^/]+) in between determines that any sequence of characters that are not `/` are matched
|
||||
"- Positive lookbehind (?<=\/) that determines that the content is preceded by `/`
|
||||
"- Positive lookahead (?=\/) that determines that the content is followed by `/
|
||||
"- ([^\/]+) in between determines that any sequence of characters that are not `/` are matched
|
||||
"- The match is put in parentheses to store the submatch
|
||||
|
||||
"The RESULTS addition stores findings in an internal table of type match_result_tab.
|
||||
"Submatches (i.e. length and offset values of the submatches) are stored in internal
|
||||
"tables themselves. Therefore, the example uses nested loops and the substring function
|
||||
"to retrieve the strings.
|
||||
LOOP AT res INTO DATA(finding).
|
||||
LOOP AT finding-submatches INTO DATA(sub).
|
||||
DATA(url_part) = substring( val = url off = sub-offset len = sub-length ).
|
||||
APPEND url_part TO url_parts.
|
||||
ENDLOOP.
|
||||
ENDLOOP.
|
||||
|
||||
"The following statement uses nested iteration expressions with FOR instead of nested
|
||||
"LOOP statements.
|
||||
DATA(url_parts_for_loop) = VALUE string_table( FOR wa1 IN res
|
||||
FOR wa2 IN wa1-submatches
|
||||
( substring( val = url off = wa2-offset len = wa2-length ) ) ).
|
||||
( substring( val = url off = wa2-offset len = wa2-length ) ) ).
|
||||
|
||||
ASSERT url_parts = url_parts_for_loop.
|
||||
*Content:
|
||||
*help.sap.com
|
||||
*docs
|
||||
|
||||
@@ -825,8 +825,7 @@ exception `CX_SY_RANGE_OUT_OF_BOUNDS` is raised.
|
||||
and
|
||||
[`substring_to`](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abensubstring_functions.htm).
|
||||
- These functions offer more options in terms of parameters, such as the use of [PCRE regular
|
||||
expressions](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenpcre_regex_glosry.htm "Glossary Entry"),
|
||||
which are covered below.
|
||||
expressions](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenpcre_regex_glosry.htm "Glossary Entry").
|
||||
- As also shown further down, using the built-in function `match`, you can extract substrings matching a given pattern.
|
||||
|
||||
Syntax examples:
|
||||
|
||||
@@ -11,6 +11,7 @@ Core data services (CDS) are an infrastructure for defining and consuming semant
|
||||
> - For cheat sheet content on [CDS entities](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abencds_entity_glosry.htm), refer to [this blog](https://blogs.sap.com/2022/10/24/feature-matrix-data-modeling-with-abap-core-data-services/).
|
||||
> - The executable example focuses on CDS view entities and covers a selection of features.
|
||||
> - The sample CDS view entities are designed to demonstrate a selection of features with a limited number of artifacts. They are not intended to be role models for proper CDS view design. They focus on syntax options only. They are not intended to solve concrete programming tasks. You should always work out your own solution for each individual case.
|
||||
> - The [ABAP Dictionary](26_ABAP_Dictionary.md) cheat sheet highlights that several [CDS entities](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abencds_entity_glosry.htm) - apart from CDS view entities - represent structured types that are usable in ABAP.
|
||||
|
||||
## A Glimpse on the CDS Syntax
|
||||
The following links take you to the source code of the cheat sheet artifacts to get a glimpse on the syntax used. To explore the syntax in action, import the ABAP cheat sheet repository into your system.
|
||||
@@ -48,6 +49,7 @@ The following links take you to the source code of the cheat sheet artifacts to
|
||||
- [ABAP CDS Development Tools: User Guide](https://help.sap.com/docs/abap-cloud/abap-cds-tools-user-guide/about-abap-cds-development-tools-user-guide?version=sap_btp)
|
||||
- [ABAP CDS Feature Tables](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abencds_language_elements.htm)
|
||||
- [ABAP CDS Glossary](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abencds_glossary.htm)
|
||||
- [ABAP CDS - SAP Annotation Documentation](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abencds_annotations_ktd_docu.htm)
|
||||
- Blogs:
|
||||
- [Feature Matrix: Data Modeling with ABAP Core Data Services](https://blogs.sap.com/2022/10/24/feature-matrix-data-modeling-with-abap-core-data-services/)
|
||||
- [ABAP CDS Cheat Sheet: Amounts and Quantities in ABAP CDS](https://blogs.sap.com/2022/07/07/abap-cds-cheat-sheet-amounts-and-quantities-in-abap-cds/)
|
||||
|
||||
@@ -28,6 +28,7 @@
|
||||
- [Getting Type Information and Creating Types/Data Objects at Runtime](#getting-type-information-and-creating-typesdata-objects-at-runtime)
|
||||
- [Ranges Tables](#ranges-tables)
|
||||
- [Typed Literals in ABAP SQL](#typed-literals-in-abap-sql)
|
||||
- [Non-Admissible Values of Literals](#non-admissible-values-of-literals)
|
||||
- [Executable Example](#executable-example)
|
||||
|
||||
## Introduction
|
||||
@@ -47,7 +48,7 @@ Data types
|
||||
> - DDIC types such as [DDIC data elements](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abendata_element_glosry.htm) (elementary data types or reference types), [DDIC structures](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenddic_structure_glosry.htm), [DDIC table types](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenddic_table_type_glosry.htm). Find more information in the [ABAP Dictionary](26_ABAP_Dictionary.md) cheat sheet.
|
||||
> - Furthermore, [database tables](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abendatabase_table_glosry.htm) and [CDS entities](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abencds_entity_glosry.htm) and their components can also be used as data types in ABAP programs.
|
||||
>
|
||||
> Data types declared in interfaces and in the public visibility section of global classes are also globally visibile. Global classes and interfaces as such are global types to refer to. In [classic ABAP](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenclassic_abap_glosry.htm), global data types can also be created in [type pools](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abentype_pool_glosry.htm). Find more information [here](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenddic_data_types.htm).
|
||||
> Data types declared in interfaces and in the public visibility section of global classes are also globally visibile. Global classes and interfaces as such are global types to refer to. In [classic ABAP](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenclassic_abap_glosry.htm), you may stumble on the option to create global data types in [type pools](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abentype_pool_glosry.htm), which is not possible in ABAP for Cloud Development. However, the predefined type pool `abap` can be used in ABAP for Cloud Development. Find more information [here](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenddic_data_types.htm).
|
||||
|
||||
|
||||
Data objects:
|
||||
@@ -103,7 +104,7 @@ For an overview, see the [ABAP Type Hierarchy](https://help.sap.com/doc/abapdocu
|
||||
|
||||
- Are composed of other types.
|
||||
- The following complex data types are available:
|
||||
- [Structured types](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenstructured_type_glosry.htm): Represent a sequence of arbitrary data type (i.e., they can be elementary, reference, or complex data types). The typical syntax element for the local definition of a structure is `... BEGIN OF ... END OF ...`.
|
||||
- [Structured types](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenstructured_type_glosry.htm): Represent a sequence of arbitrary data types (i.e., they can be elementary, reference, or complex data types). The typical syntax element for the local definition of a structure is `... BEGIN OF ... END OF ...`.
|
||||
- [Table types](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abentable_type_glosry.htm): Consist of a sequence of any number of lines of the same data type. It can be any elementary type, reference type, or complex data type. The type definition includes other properties such as the [table category](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abentable_category_glosry.htm) (defines how tables can be accessed) and [table key](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abentable_key_glosry.htm) (to identify the table lines). The typical syntax element is `... TABLE OF ...`.
|
||||
- [Enumerated types](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenenum_type_glosry.htm): Specify a set of values in addition to the actual type properties. The typical syntax element is `... BEGIN OF ENUM ... END OF ENUM ...`. See more information [here](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenenumerated_types_usage.htm) and further down.
|
||||
- [Mesh types](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenmesh_type_glosry.htm): Special structured type that contains only table types with structured line types as components that can be linked using mesh associations. The typical syntax element is `... BEGIN OF MESH ... END OF MESH ...`. See more information [here](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abaptypes_mesh.htm).
|
||||
@@ -322,7 +323,11 @@ TYPES tr_like_table_ref LIKE TABLE OF REF TO itab_str.
|
||||
- [Generic ABAP Types](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenbuilt_in_types_generic.htm)
|
||||
- [ABAP cheat sheet about dynamic programming](06_Dynamic_Programming.md) regarding field symbols and `ASSIGN` statements
|
||||
|
||||
The following code snippet demonstrates generic types with field symbols.
|
||||
> **💡 Note**<br>
|
||||
> The `TYPE REF TO` addition types as a 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.
|
||||
|
||||
|
||||
The following code snippet demonstrates generic types with field symbols. For complete and generic typing of formal parameters, refer to the [ABAP Object Orientation](04_ABAP_Object_Orientation.md#complete-typing-of-formal-parameters) cheat sheet.
|
||||
|
||||
```abap
|
||||
FIELD-SYMBOLS:
|
||||
@@ -356,9 +361,8 @@ FIELD-SYMBOLS:
|
||||
<table> TYPE table, "Standard table
|
||||
|
||||
"Other types
|
||||
<simple> TYPE simple, "Elementary data type including enumerated types and
|
||||
"structured types with exclusively character-like flat components
|
||||
<object> TYPE REF TO object. "object can only be specified after REF TO; can point to any object
|
||||
<simple> TYPE simple. "Elementary data type including enumerated types and
|
||||
"structured types with exclusively character-like flat components
|
||||
|
||||
"Data objects to work with
|
||||
DATA: BEGIN OF s,
|
||||
@@ -378,7 +382,6 @@ DATA: BEGIN OF s,
|
||||
xl1 TYPE x LENGTH 1,
|
||||
xstr TYPE xstring,
|
||||
structure TYPE zdemo_abap_carr, "character-like flat structure
|
||||
oref TYPE REF TO object,
|
||||
END OF s.
|
||||
|
||||
"The following static ASSIGN statements demonstrate various assignments
|
||||
@@ -467,12 +470,6 @@ ASSIGN s-date TO <simple>.
|
||||
ASSIGN s-structure TO <simple>.
|
||||
ASSIGN s-xl1 TO <simple>.
|
||||
"ASSIGN s-tab_ha TO <simple>.
|
||||
|
||||
s-oref = NEW zcl_demo_abap_objects( ).
|
||||
ASSIGN s-oref TO <object>.
|
||||
"Accessing class attributes using casting
|
||||
DATA(publ_str) = CAST zcl_demo_abap_objects( <object> )->public_string.
|
||||
CAST zcl_demo_abap_objects( <object> )->another_string = `ABAP`.
|
||||
```
|
||||
|
||||
<p align="right"><a href="#top">⬆️ back to top</a></p>
|
||||
@@ -661,7 +658,7 @@ An assignment passes the contents of a source to a target data object.
|
||||
|
||||
> **💡 Note**<br>
|
||||
> - There are conversion rules when assigning a source to a target data object that have different types. For more information, see the topic [Assignment and Conversion Rules](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenconversion_rules.htm) in the ABAP Keyword Documentation, especially for complex types, since elementary types are usually demonstrated in the cheat sheet.
|
||||
> - There are many ways to assigning values to data objects in ABAP. Assignments with the assignment operator `=` are mostly used here.
|
||||
> - There are many ways to assigning values to data objects in ABAP. They occur in the context of various ABAP statements. Here, assignments with the assignment operator `=` are mostly used.
|
||||
> - In older ABAP code, you may see `MOVE ... TO ...` statements for value assignments. These statements are obsolete. They are not to be confused with `MOVE-CORRESPONDING` statements for complex types. These are not obsolete.
|
||||
|
||||
The following code snippet shows several ways to assign values to data objects.
|
||||
@@ -1934,6 +1931,36 @@ SELECT SINGLE
|
||||
|
||||
<p align="right"><a href="#top">⬆️ back to top</a></p>
|
||||
|
||||
### Non-Admissible Values of Literals
|
||||
|
||||
Note recent syntax warnings when using literals that represent invalid values for target types. The following example demonstrates the assignement of literals using admissible and non-admissible values. You can copy and paste the code into a demo class in your SAP BTP ABAP Environment to explore the syntax warnings.
|
||||
|
||||
|
||||
```abap
|
||||
DATA char3 TYPE c LENGTH 3.
|
||||
|
||||
"Value is admissable and convertible
|
||||
char3 = 'abc'.
|
||||
|
||||
"Non-admissable value assigned to the target (type c length 6)
|
||||
char3 = 'defghi'.
|
||||
|
||||
DATA date TYPE d.
|
||||
|
||||
"Value is admissable and convertible
|
||||
date = '20250101'.
|
||||
|
||||
"Non-admissable value assigned to the target
|
||||
"Type i
|
||||
date = 20250101.
|
||||
"More characters than type d
|
||||
date = '20250101234'.
|
||||
"Fewer characters than type d
|
||||
date = '202511'.
|
||||
```
|
||||
|
||||
<p align="right"><a href="#top">⬆️ back to top</a></p>
|
||||
|
||||
## Executable Example
|
||||
|
||||
[zcl_demo_abap_dtype_dobj](./src/zcl_demo_abap_dtype_dobj.clas.abap)
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
- [Information about Non-Initial Structure Components](#information-about-non-initial-structure-components)
|
||||
- [Comparing Content of Compatible Internal Tables](#comparing-content-of-compatible-internal-tables)
|
||||
- [Dynamic Programming](#dynamic-programming)
|
||||
- [Context Information](#context-information)
|
||||
- [Getting the User Name](#getting-the-user-name)
|
||||
- [XML/JSON](#xmljson)
|
||||
- [ABAP Repository Object Information](#abap-repository-object-information)
|
||||
- [Generating ABAP Repository Objects](#generating-abap-repository-objects)
|
||||
@@ -41,6 +41,7 @@ This ABAP cheat sheet contains a selection of [released](https://help.sap.com/do
|
||||
> **💡 Note**<br>
|
||||
> - The cheat sheet is not a comprehensive overview, and the code snippets do not claim to be comprehensive as far as options, methods, or parameters are concerned. It is intended to give you a rough overview, for you to get an idea. It is an invitation to a more in-depth exploration.
|
||||
> - For more information and where available, refer to the class documentation (for example, choose F2 when the cursor is on the class name in ADT), the ABAP Keyword Documentation, and the SAP Help Portal documentation.
|
||||
> - You might find that different classes can achieve similar or the same results, especially with the Extension Components Library (XCO), a general-purpose development library designed specifically for ABAP for Cloud Development. Choose the classes that best meet your needs.
|
||||
> - [Disclaimer](./README.md#%EF%B8%8F-disclaimer)
|
||||
|
||||
## Excursion: Available Classes in ABAP for Cloud Development
|
||||
@@ -1099,6 +1100,22 @@ DATA(repl_result_not_extended) = matcher_not_extended->text.
|
||||
<td> Class </td> <td> Details/Code Snippet </td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td> <code>CL_ABAP_CONTEXT_INFO</code> </td>
|
||||
<td>
|
||||
Provides context information relevant to the current ABAP session.
|
||||
<br><br>
|
||||
|
||||
``` abap
|
||||
"Getting current date in UTC (not the system or user time), e.g. 20240101
|
||||
DATA(sys_date) = cl_abap_context_info=>get_system_date( ).
|
||||
|
||||
"Getting current time in UTC, e.g. 152450
|
||||
DATA(sys_time) = cl_abap_context_info=>get_system_time( ).
|
||||
```
|
||||
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td> <code>CL_ABAP_DATFM</code> </td>
|
||||
<td>
|
||||
@@ -1927,7 +1944,7 @@ ENDTRY.
|
||||
|
||||
<p align="right"><a href="#top">⬆️ back to top</a></p>
|
||||
|
||||
## Context Information
|
||||
## Getting the User Name
|
||||
|
||||
<table>
|
||||
<tr>
|
||||
@@ -1940,12 +1957,6 @@ Provides context information relevant to the current ABAP session.
|
||||
<br><br>
|
||||
|
||||
``` abap
|
||||
"Getting current date in UTC (not the system or user time), e.g. 20240101
|
||||
DATA(sys_date) = cl_abap_context_info=>get_system_date( ).
|
||||
|
||||
"Getting current time in UTC, e.g. 152450
|
||||
DATA(sys_time) = cl_abap_context_info=>get_system_time( ).
|
||||
|
||||
"User alias, e.g. XY0000001234
|
||||
DATA(alias) = cl_abap_context_info=>get_user_alias( ).
|
||||
"You can also get user information using XCO classes
|
||||
@@ -1956,6 +1967,14 @@ TRY.
|
||||
DATA(formatted_name) = cl_abap_context_info=>get_user_formatted_name( ).
|
||||
CATCH cx_abap_context_info_error.
|
||||
ENDTRY.
|
||||
|
||||
"The class also provides the option to retrieve the current date and time
|
||||
"in UTC.
|
||||
"Getting current date in UTC (not the system or user time), e.g. 20240101
|
||||
DATA(sys_date) = cl_abap_context_info=>get_system_date( ).
|
||||
|
||||
"Getting current time in UTC, e.g. 152450
|
||||
DATA(sys_time) = cl_abap_context_info=>get_system_time( ).
|
||||
```
|
||||
|
||||
</td>
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
- [Case Conversions in Replacement Patterns](#case-conversions-in-replacement-patterns)
|
||||
- [Setting Options and Control Verbs](#setting-options-and-control-verbs)
|
||||
- [Callouts](#callouts)
|
||||
- [Coniditional Patterns](#coniditional-patterns)
|
||||
- [Conditional Patterns](#conditional-patterns)
|
||||
- [ABAP Statements Using Regular Expressions](#abap-statements-using-regular-expressions)
|
||||
- [Built-In Functions in ABAP Using Regular Expressions](#built-in-functions-in-abap-using-regular-expressions)
|
||||
- [Built-In Functions in ABAP SQL and CDS Using Regular Expressions](#built-in-functions-in-abap-sql-and-cds-using-regular-expressions)
|
||||
@@ -128,7 +128,7 @@ Note that some of the example strings are represented by string templates.
|
||||
| `\R` | Line feed sequence, ensuring that regular expression matches all kinds of newlines, such as `\n`, `\r`, or `\r\n` (carriage return followed by line feed) | `\R.` | <code>\|abc\rdef\nghi\r\njkl mno\|</code> | <code>\|\rd\|</code>, <code>\|\ng\|</code>, <code>\|\r\nj\|</code> | <code>\| m\|</code> |
|
||||
| `\x{...}` | Character with hex code | `\x{00A0}#` (non-breaking space and #) | <code>\|#{ cl_abap_conv_codepage=>create_in( codepage = \`UTF-16BE\` )->convert( source = CONV xstring( \`00A0\` ) ) }#\|</code> | The non-breaking space plus the second # character | The first # character |
|
||||
| `\N{U+...}` | Character with Unicode code point | `\N{U+00A0}#` (non-breaking space and # as above) | See the example below | See the example below | |
|
||||
| `\p{..}` | Character with a specified Unicode character property; see the [documentation](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/abenregex_pcre_syntax_specials.html) for options; among them, there are, for example, properties for lower (`Ll`) and uppercase (`Lu`) letters; negation: `\P{..}` | 1. `\p{Ll}+` <br> 2. `\p{Lu}+` <br> 3. `\P{Lu}+` | Hello ABAP | 1. H<ins>**ello**</ins> ABAP <br> 2. <ins>**H**</ins>ello <ins>**ABAP**</ins> <br> 3. H<ins>**ello **</u></ins>ABAP (the space is also matched) | 1. Upper case letter sequences <br> 2. Lower case letter sequences <br> 3. Upper case letter sequences |
|
||||
| `\p{..}` | Character with a specified Unicode character property; see the [documentation](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/abenregex_pcre_syntax_specials.html) for options; among them, there are, for example, properties for lowercase (`Ll`) and uppercase (`Lu`) letters; negation: `\P{..}` | 1. `\p{Ll}+` <br> 2. `\p{Lu}+` <br> 3. `\P{Lu}+` | Hello ABAP | 1. H<ins>**ello**</ins> ABAP <br> 2. <ins>**H**</ins>ello <ins>**ABAP**</ins> <br> 3. H<ins>**ello **</u></ins>ABAP (the space is also matched) | 1. Upper case letter sequences <br> 2. Lower case letter sequences <br> 3. Upper case letter sequences |
|
||||
|
||||
Examples:
|
||||
|
||||
@@ -391,7 +391,7 @@ string_capt_group = replace( val = `APAB` pcre = `(..)(..)` with = `$2$1` ).
|
||||
string_capt_group = replace( val = `abcd` pcre = `(..)(..)` with = `#$0#` ).
|
||||
|
||||
"Alternative replacement syntax with the curly brackets
|
||||
"cdab
|
||||
"ABAP
|
||||
string_capt_group = replace( val = `APAB` pcre = `(..)(..)` with = `${2}${1}` ).
|
||||
|
||||
"------- Named capturing groups -------
|
||||
@@ -753,7 +753,7 @@ ENDCLASS.
|
||||
|
||||
<p align="right"><a href="#top">⬆️ back to top</a></p>
|
||||
|
||||
### Coniditional Patterns
|
||||
### Conditional Patterns
|
||||
|
||||
- Used to check whether certain capture groups are matched
|
||||
- When matched or not matched, you can specify replacement patterns; you can also specify replacement patterns with conditions
|
||||
@@ -1224,7 +1224,7 @@ DATA(regex) = cl_abap_regex=>create_pcre( pattern = `\D\d`
|
||||
"- create_pcre method of the cl_abap_matcher class
|
||||
"Note that several importing parameters are available to enable
|
||||
"further settings of the regular expression, e.g. ignoring the
|
||||
"case, using the extended mode, etc. The example pass a string
|
||||
"case, using the extended mode, etc. The examples pass a string
|
||||
"to the 'text' parameter. You can also specify internal tables
|
||||
"with the 'table' parameter and more.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user