This commit is contained in:
danrega
2025-01-13 12:49:30 +01:00
parent 6926ca4f3b
commit 401542476a
8 changed files with 863 additions and 107 deletions

View File

@@ -3146,6 +3146,7 @@ DATA(line_ref) = REF #( itab[ 3 ] ).
- You can specify default values for lines that are not found to avoid an exception.
- The first example shows catching the execption with a `TRY` control structure.
- The `OPTIONAL` and `DEFAULT` additions can be used in the context of statements using table expressions and constructor expressions (`VALUE` and `REF` are possible).
- When using `OPTIONAL`, the type-specific initial value is set. After the `DEFAULT` addition, a value is expected that is convertible to the target type. You may also specify another table expression for an alternative table line to be searched.
<br>

View File

@@ -10,6 +10,8 @@
- [Creating Structures](#creating-structures)
- [Creating Structures Using Existing Structured Types](#creating-structures-using-existing-structured-types)
- [Creating Structures by Inline Declaration](#creating-structures-by-inline-declaration)
- [Creating Constant and Immutable Structures](#creating-constant-and-immutable-structures)
- [Creating Enumerated Structures](#creating-enumerated-structures)
- [Creating Anonymous Structures](#creating-anonymous-structures)
- [Variants of Structures](#variants-of-structures)
- [Accessing (Components of) Structures](#accessing-components-of-structures)
@@ -251,6 +253,67 @@ LOOP AT itab INTO DATA(wa_2).
ENDLOOP.
```
<p align="right"><a href="#top">⬆️ back to top</a></p>
### Creating Constant and Immutable Structures
- Constant structures can be created with the `... BEGIN OF ... END OF ...` additions. Their values cannot be changed.
- As shown above, the [`FINAL`](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenfinal_inline.htm) declaration operator is used to create [immutable variables](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenimmutable_variable_glosry.htm).
```abap
CONSTANTS: BEGIN OF const_struct,
num TYPE i VALUE 123,
str TYPE string VALUE `ABAP`,
n3 TYPE n LENGTH 3 VALUE '000',
c5 TYPE c LENGTH 5 VALUE 'abcde',
END OF const_struct.
DATA(num) = const_struct-num.
"const_struct-num = 456.
TYPES struct_type LIKE const_struct.
FINAL(final_struct) = VALUE struct_type( num = 987 str = `hello` n3 = '123' c5 = 'xyz' ).
DATA(num_from_final) = final_struct-num.
"final_struct-num = 1.
SELECT * FROM zdemo_abap_carr INTO TABLE @DATA(itab).
"The work area is specified as immutable variable. The variable's content cannot be changed
"in the loop, however, the variable is exchanged with the next loop pass.
LOOP AT itab INTO FINAL(wa).
DATA(carrid) = wa-carrid.
"wa-carrid = 'XY'.
ENDLOOP.
```
<p align="right"><a href="#top">⬆️ back to top</a></p>
### Creating Enumerated Structures
Find more information on [enumerated types](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenenum_type_glosry.htm) in the [Data Types and Data Objects](16_Data_Types_and_Objects.md#abap-enumerated-types-and-objects) cheat sheet.
```abap
"When creating enumerated types, an enumerated structure can optionally be declared in
"the context of the type declaration.
"A component of an enumerated structure: An enumerated constant that exists as a component
"of a constant structure, not as a single data object.
TYPES basetype TYPE i.
TYPES: BEGIN OF ENUM t_enum_struc STRUCTURE en_struc BASE TYPE basetype,
a VALUE IS INITIAL,
b VALUE 1,
c VALUE 2,
d VALUE 3,
END OF ENUM t_enum_struc STRUCTURE en_struc.
DATA(enum_comp) = en_struc-b.
DATA(conv_enum_comp) = CONV basetype( en_struc-b ).
ASSERT conv_enum_comp = 1.
```
<p align="right"><a href="#top">⬆️ back to top</a></p>
### Creating Anonymous Structures
Using the instance operator [`NEW`](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenconstructor_expression_new.htm) and [`CREATE DATA`](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abapcreate_data.htm) statements, you can create [anonymous data objects](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenanonymous_data_object_glosry.htm "Glossary Entry"), such as anonymous structures.

File diff suppressed because it is too large Load Diff

View File

@@ -430,6 +430,9 @@ ASSIGN s-xl1 TO <simple>.
ASSIGN s-oref TO <object>.
```
> **💡 Note**<br>
> After `TYPE REF TO`, the only generic types you can specify are `data` for fully generic data reference variables and `object` for fully generic object reference variables.
<p align="right"><a href="#top">⬆️ back to top</a></p>
### Data References

View File

@@ -9,6 +9,7 @@
- [`IF` Statements](#if-statements)
- [Excursion: `COND` Operator](#excursion-cond-operator)
- [`CASE`: Case Distinctions](#case-case-distinctions)
- [Control Structures Using CASE TYPE OF](#control-structures-using-case-type-of)
- [Excursion: `SWITCH` Operator](#excursion-switch-operator)
- [Loops](#loops)
- [`DO`: Unconditional Loops](#do-unconditional-loops)
@@ -355,6 +356,10 @@ CASE random_num.
ENDCASE.
```
<p align="right"><a href="#top">⬆️ back to top</a></p>
#### Control Structures Using CASE TYPE OF
Special control structure introduced by [`CASE TYPE OF`](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abapcase_type.htm): Checks the type of object reference variables. An object reference variable with the static type of a class or an interface must be specified after `CASE TYPE OF`.
```abap
@@ -547,7 +552,7 @@ Further keywords for defining loops are as follows. They are not dealt with here
- [`LOOP ... ENDLOOP`](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abaploop_at_itab.htm) statements are meant for loops across internal tables. See also the cheat sheet on internal tables.
- In contrast to the loops above, the system field `sy-index` is not set. Instead, the system field `sy-tabix` is set and which contains the table index of the current table line in the loop pass.
- You can also realize loops using iteration expressions with `VALUE` and `REDUCE`. For more information, refer to the [Constructor Expressions](05_Constructor_Expressions.md) cheat sheet.
- `FOR` loops: You can also realize loops using iteration expressions with `VALUE` and `REDUCE`. For more information, refer to the [Constructor Expressions](05_Constructor_Expressions.md) cheat sheet.
- [`SELECT ... ENDSELECT`](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abapselect.htm) statements loop across the result set of a database access. See also the cheat sheet on ABAP SQL.
<p align="right"><a href="#top">⬆️ back to top</a></p>

View File

@@ -166,6 +166,8 @@ TYPES te_utc TYPE utclong.
"You might also stumble on a length specification in parentheses following the data type name.
"It is recommended that you use addition LENGTH instead of the parentheses.
TYPES te_cfour(4) TYPE c.
TYPES te_nfive(5) TYPE n.
TYPES te_xtwo(2) TYPE x.
"**** Data type declarations based on existing types or data objects ****
@@ -509,7 +511,9 @@ DATA: do_i TYPE i,
do_str TYPE string,
"Specifying the length in parantheses instead of using the
"LENGTH addition is not recommended
do_ctwo(2) TYPE c.
do_ctwo(2) TYPE c,
do_nthree(3) TYPE n,
do_xfour(4) TYPE x.
"Referring to locally declared data types
TYPES te_string TYPE string.
@@ -1461,7 +1465,7 @@ ENDCLASS.
<p align="right"><a href="#top">⬆️ back to top</a></p>
### ABAP Enumerated Types and Objects
## ABAP Enumerated Types and Objects
- ABAP supports the concept of enumerations.
- Enumerations are a mixture of types and constants.
- An [enumerated type](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenenum_type_glosry.htm) specifies a value set in addition to the actual type properties.
@@ -1789,6 +1793,9 @@ do_h_c3 = 'abc'.
"here like this) is not an admissable value for the target type.
do_h_c3 = 'defghi'.
"'j' assigned, but length and memory do not change
do_h_c3 = 'j'.
"Memory consumption changes for dynamic data objects
do_i_str = `abc`.
do_i_str = `d`.
@@ -2065,6 +2072,37 @@ DATA itab5 TYPE zcl_demo_abap_amdp=>carr_tab.
<tr>
<td>
`TYPE BEGIN OF`
</td>
<td>
``` abap
"------------ TYPE BEGIN OF ----------------
"Structured types and data objects created with TYPE BEGIN OF ... END OF ...
TYPES: BEGIN OF some_struct_type,
comp1 TYPE i,
comp2 TYPE string,
comp3 TYPE c LENGTH 3,
END OF some_struct_type.
DATA: BEGIN OF some_struct,
compa TYPE n LENGTH 5,
compb TYPE xstring,
compc TYPE p LENGTH 8 DECIMALS 2,
END OF some_struct.
```
</td>
</tr>
<tr>
<td>
`TYPE TABLE OF`
</td>
@@ -2488,7 +2526,7 @@ SELECT SINGLE
### Non-Admissible Values of Literals
Note recent syntax warnings when using literals that represent invalid values for target types. The following example demonstrates the assignment 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.
Syntax warnings are displayed when using literals that represent invalid values for target types. The following example demonstrates the assignment 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

View File

@@ -2344,7 +2344,7 @@ DATA table_json_to_abap TYPE string_table.
<ul>
<li>Provides access to abstractions for ABAP Dictionary objects such as database tables, data elements, types and more.</li>
<li>For more detailed examples, refer to the <a href="https://help.sap.com/docs/btp/sap-business-technology-platform/overview-of-xco-modules">SAP Help Portal</a>. See also the executable example of the ABAP for Cloud Development cheat sheet.</li>
<li>For more detailed examples, refer to the <a href="https://help.sap.com/docs/btp/sap-business-technology-platform/overview-of-xco-modules">SAP Help Portal</a>. See also the executable example of the <a href="19_ABAP_for_Cloud_Development.md">ABAP for Cloud Development</a> cheat sheet.</li>
<li>The code snippet contains a small selection and only hints at the many ways to retrieve different pieces of information that the classes provide.</li>
<li>The snippet does not only include the <code>XCO_CP_ABAP_REPOSITORY</code> class but also others such as <code>XCO_CP_ABAP</code>, <code>XCO_CP_ABAP_SQL</code>, and <code>XCO_CP_CDS</code>. More XCO classes are available in that context.</li>
</ul>
@@ -2704,7 +2704,9 @@ DATA(sub_acc_id) = ten->get_subaccount_id( )->as_string( ).
<tr>
<td> <code>CX_*</code> </td>
<td>
Exception classes are special classes, usually starting with the name <code>CX_*</code>, that serve as the basis for <a href="https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abencatchable_exception_glosry.htm">catchable exceptions</a>. When an exception is raised, an object of such an exception class is created. There are several predefined exception classes. Find more information in the [Exceptions and Runtime Errors](27_Exceptions.md) cheat sheet.
<br><br>
``` abap
@@ -3301,6 +3303,8 @@ To check out examples in demo classes, expand the collapsible sections below.
<summary>🟢 1. Read example: Retrieving ABAP cheat sheet markdown content using a GitHub API and sending a ZIP file with the content via email</summary>
<!-- -->
<br>
> **⚠️ Note/Disclaimer**<br>
> - The following self-contained and oversimplified example is not a representative best practice example, nor does it cover a meaningful use case. It only explores method calls and is intended to give a rough idea of the functionality.</li>
> - The example uses the <code>create_by_url</code> method, which is only suitable for public services or testing purposes. No authentication is required for the APIs used.
@@ -3465,6 +3469,8 @@ ENDCLASS.
<summary>🟢 2. Post example: Demonstrating a post request by converting Markdown to HTML using the GitHub API</summary>
<!-- -->
<br>
> **⚠️ Note/Disclaimer**<br>
> - As stated for the previous example, also note for this example: Before using the GitHub APIs, make sure that you have consulted the following documentation: <a href="https://docs.github.com/en">GitHub Docs</a>, <a href="https://docs.github.com/en/enterprise-cloud@latest/rest/markdown/markdown?apiVersion=2022-11-28#render-a-markdown-document">Render a Markdown document</a>, <a href="https://docs.github.com/en/rest/using-the-rest-api/rate-limits-for-the-rest-api?apiVersion=2022-11-28">Rate limits for the REST API</a>
> - To run the example class, copy and paste the code into a class named `zcl_demo_abap`. Run the class using F9. It is set up to display HTML content in the console. Using the GitHub API, sample Markdown content is sent and converted to HTML.

View File

@@ -14,6 +14,7 @@
- [RETRY Statements](#retry-statements)
- [RESUME Statements and RESUMABLE Additions](#resume-statements-and-resumable-additions)
- [Using Messages as Exception Texts](#using-messages-as-exception-texts)
- [System Interfaces for Messages](#system-interfaces-for-messages)
- [Excursion: MESSAGE Statements](#excursion-message-statements)
- [Syntax Variants of RAISE EXCEPTION/THROW](#syntax-variants-of-raise-exceptionthrow)
- [Runtime Errors](#runtime-errors)
@@ -851,6 +852,13 @@ ENDLOOP.
- If an exception is raised and ...
- not handled, the exception text is displayed in the [short dump](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenshort_dump_glosry.htm) of the runtime error.
- handled, the text can be retrieved using the `get_text` method as shown above.
- As outlined in the next section, exception classes must implement one of the system interfaces for messages to use exception texts.
<p align="right"><a href="#top">⬆️ back to top</a></p>
### System Interfaces for Messages
- Exception classes must implement one of the system interfaces for messages to use exception texts:
- `IF_T100_MESSAGE`:
- Contains the `T100KEY` structured attribute (type `SCX_T100KEY`), specifying a message in the `T100` database table. Components are: `msgid` for the message class, `msgno` for the message number, and `attr1`/`attr2`/`attr3`/`attr4` for potential placeholders in message texts.