-
Notifications
You must be signed in to change notification settings - Fork 237
/
zcl_demo_abap_dtype_dobj.clas.abap
2042 lines (1595 loc) · 80.7 KB
/
zcl_demo_abap_dtype_dobj.clas.abap
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
***********************************************************************
*
* ABAP cheat sheet: Data Types and Data Objects
*
* -------------------------- PURPOSE ----------------------------------
* - Example to demonstrate data types and data objects.
* - Note that in many cases there is no output displayed because the
* focus is on syntax options and declarations. In the class, you can
* set breakpoints and use the debugger to check out data objects.
* You can also use the F2 information for the many types and data
* objects. Simply select a type or object in the code and press F2
* in ADT to check out the information.
*
* ----------------------- GETTING STARTED -----------------------------
* - Open the class with the ABAP development tools for Eclipse (ADT).
* - Choose F9 to run the class.
* - Check the console output.
* - To understand the context and the ABAP syntax used, refer to the
* notes included in the class as comments or refer to the respective
* topic in the ABAP Keyword Documentation.
* - Due to the amount of console output, the examples contain numbers
* (e.g. 1) ..., 2) ..., 3) ...) for the individual example sections.
* Also, the variable name is displayed in most cases. So to find
* the relevant output in the console easier and faster, just search
* for the number/variable name in the console (CTRL+F in the console)
* or use the debugger.
*
* ----------------------------- NOTE -----------------------------------
* The code presented in this class is intended only to support the ABAP
* cheat sheets. It is not intended for direct use in a production system
* environment. The code examples in the ABAP cheat sheets are primarily
* intended to provide a better explanation and visualization of the
* syntax and semantics of ABAP statements, not to solve concrete
* programming tasks. For production application programs, you should
* always work out your own solution for each individual case. There is
* no guarantee for the correctness or completeness of the code.
* Furthermore, there is no legal responsibility or liability for any
* errors or their consequences that may occur when using the the example
* code.
*
***********************************************************************
"! <p class="shorttext synchronized">ABAP cheat sheet: Data Types and Data Objects</p>
"! Example to demonstrate data types and data objects in ABAP.<br>Choose F9 in ADT to run the class.
CLASS zcl_demo_abap_dtype_dobj DEFINITION
PUBLIC
FINAL
CREATE PUBLIC .
PUBLIC SECTION.
INTERFACES: if_oo_adt_classrun.
CLASS-METHODS:
class_constructor.
TYPES t_pub_text_c30 TYPE c LENGTH 30.
CONSTANTS: comma TYPE string VALUE `, `.
CLASS-DATA: read_only_attribute TYPE string VALUE `Hallo` READ-ONLY.
PROTECTED SECTION.
PRIVATE SECTION.
TYPES t_prv_text_c30 TYPE c LENGTH 30.
CLASS-DATA cl_text TYPE t_prv_text_c30.
DATA text TYPE t_pub_text_c30 VALUE '!!!'.
METHODS adapt_text RETURNING VALUE(str) TYPE string.
METHODS addition_with_generic_num IMPORTING num1 TYPE numeric
num2 TYPE numeric
RETURNING VALUE(result) TYPE decfloat34.
constants no_output type string value `No output for this section. Check out the types in the code e.g. using the F2 information.`.
**********************************************************************
"Types and methods for demonstrating enumerated types and objects
"The definition of an enumerated type in ABAP declares its enumerated constants (these are special enumerated objects).
"a) In the case below, no explicit base type is specified. Then, the standard base type of the constants is i. The
" enumerated values are counted up starting with 0 (a -> 0, b -> 1 ...).
TYPES: BEGIN OF ENUM t_enum,
a,
b,
c,
d,
END OF ENUM t_enum.
"b) For the following enumerated type, an explicit base type is specified and start values provided using the VALUE addition
" Note that one value must be initial.
TYPES: basetype TYPE c LENGTH 2,
BEGIN OF ENUM t_enum_base BASE TYPE basetype,
"If VALUE is specified explicitly, VALUE IS INITIAL must be used exactly once.
e VALUE IS INITIAL,
f VALUE 'u',
g VALUE 'v',
h VALUE 'wx',
i VALUE 'yz',
END OF ENUM t_enum_base.
"c) Optionally an enumerated structure can be declared in the context of the type declaration.
"Use case: If you have more than one enumerated type within one context. In doing so, you declare a constant enumeration structure.
"The components of the structure are the enumeration constants of the enumerated type.
TYPES: BEGIN OF ENUM t_enum_struc STRUCTURE en_struc BASE TYPE basetype,
j VALUE IS INITIAL,
k VALUE 'hi',
l VALUE 'ab',
m VALUE 'ap',
END OF ENUM t_enum_struc STRUCTURE en_struc.
METHODS enum_meth_params IMPORTING char TYPE t_enum
RETURNING VALUE(output) TYPE string.
METHODS enum_processing RETURNING VALUE(output) TYPE string_table.
METHODS rtti_enum RETURNING VALUE(output) TYPE string_table.
ENDCLASS.
CLASS zcl_demo_abap_dtype_dobj IMPLEMENTATION.
METHOD adapt_text.
DATA text TYPE t_pub_text_c30.
text = cl_text && comma && sy-uname && me->text.
str = text && | (Note: The value of me->text is "{ me->text }")|.
ENDMETHOD.
METHOD addition_with_generic_num.
result = num1 + num2.
ENDMETHOD.
METHOD class_constructor.
"Filling demo database tables.
zcl_demo_abap_aux=>fill_dbtabs( ).
ENDMETHOD.
METHOD enum_meth_params.
CASE char.
WHEN a.
output = a.
WHEN b.
output = b.
WHEN OTHERS.
output = `Either c or d: ` && char.
ENDCASE.
ENDMETHOD.
METHOD enum_processing.
"Read and write positions of enumerated objects
"Enumerated objects can be used in all read positions in which the operand
"type is their enumerated type.
"Likewise, enumerated variables can only be used in write positions in which
"the operand type is the enumerated type and only the associated enumerated
"values can be written.
"So, assignments are possible only from one enumerated type to the same (with one
"exception -> assignment to character-like variables of the types c and string)
DATA do_enum TYPE t_enum.
do_enum = a.
APPEND |do_enum: { do_enum }| TO output.
DATA do_enum_2 LIKE do_enum.
do_enum_2 = do_enum.
APPEND |do_enum_2: { do_enum_2 }| TO output.
"Assignment to character-like variables of the types c and string.
"In this case, the target field is assigned the name of the enumerated constant or
"the component of the enumerated structure under which the enumerated value of the
"source field is defined in the enumerated type.
DATA do_a_string TYPE string.
do_a_string = do_enum.
APPEND |do_a_string: { do_a_string }| TO output.
"Or using the CONV operator as follows
DATA(do_next_string) = CONV string( do_enum ).
APPEND |do_next_string: { do_next_string }| TO output.
"Enumerated constants are converted implicitly to the type string
"before the concatenation in the string template.
DATA(str_from_enum) = |{ a }{ b }{ c }{ d }|.
APPEND |str_from_enum: { str_from_enum }| TO output.
"Note that only the enumerated type itself is relevant. Usually, the content
"of an enumerated object is not of interest.
"The enumerated value in the base type can be accessed using the constructor
"operators CONV and EXACT only. The base type is i in this case.
DATA(conv_value) = CONV i( do_enum ).
APPEND |conv_value: { conv_value }| TO output.
"Converting the other way round.
DATA(another_conv) = CONV t_enum( 3 ).
APPEND |another_conv: { another_conv }| TO output.
"If known statically, an attempt to assign a value other than a valid enumerated value
"to an enumerated variable produces a syntax error.
"If not known statically, an exception is raised.
"The following produces a syntax error
"do_enum = f.
"The following example shows raising an exception.
DATA dobj TYPE t_enum.
TYPES t_int_tab TYPE TABLE OF i WITH EMPTY KEY.
DATA(int_tab) = VALUE t_int_tab( ( 0 ) ( 1 ) ( 2 ) ( 3 ) ( 4 ) ).
DATA str_tab TYPE TABLE OF string.
LOOP AT int_tab INTO DATA(wa_en).
TRY.
dobj = CONV t_enum( wa_en ).
APPEND dobj TO str_tab.
CATCH cx_sy_conversion_no_enum_value INTO DATA(error_enum).
APPEND error_enum->get_text( ) TO str_tab.
ENDTRY.
ENDLOOP.
APPEND `------------- START: Output for str_tab -------------` TO output.
APPEND LINES OF str_tab TO output.
APPEND `^^^^^^^^^^^^^ END: Output for str_tab ^^^^^^^^^^^^^` TO output.
"An enumerated variable can be set to the initial value of its base type
"using CLEAR.
CLEAR do_enum.
APPEND |do_enum: { do_enum }| TO output.
"Enumerated structures
DATA do_enum_s TYPE t_enum_struc.
"The enumerated structure en_struc was decalred in the public section.
"Using the addition LIKE, a second structure is created referring to the enumerated structure.
"Note that the second structure is not a constant structure.
"The components of the constant structure contain the enumerated values of the enumerated type.
"All the components of the variable structure declared by LIKE contain the initial values.
DATA do_s LIKE en_struc.
APPEND |do_s: { do_s-j } / { do_s-k } / { do_s-l } / { do_s-m }| TO output.
DATA(do_en) = en_struc.
APPEND |do_en: { do_en-j } / { do_en-k } / { do_en-l } / { do_en-m }| TO output.
"Accessing structure components using the component selector
DATA(do_en_k) = en_struc-k.
APPEND |do_en_k: { do_en_k }| TO output.
DATA(do_s_m) = do_s-m.
APPEND |do_s_m: { do_s_m }| TO output.
"Assigning enumerated constants to the variable structure
do_s = en_struc.
APPEND |do_s: { do_s-j } / { do_s-k } / { do_s-l } / { do_s-m }| TO output.
ENDMETHOD.
METHOD if_oo_adt_classrun~main.
out->write( |ABAP Cheat Sheet Example: Data Types and Data Objects\n\n| ).
**********************************************************************
out->write( |Declaring data types\n\n| ).
"The following examples deal with the declaration of data types.
"They show how data types can be declared locally in an ABAP program.
"Note:
"- Data types can also be declared in the ABAP Dictionary (DDIC) or as a
" CDS entity, which is not covered in this demo example.
"- In ADT and because of the many type declarations, you may want to press
" F2 on the types to get more information.
"- The examples show a selection.
"- Only non-generic types can be used.
out->write( |1) Declaring data types based on elementary types\n\n| ).
"See the ABAP Keyword Documentation for the value ranges that are
"accepted by these types.
"Data type declarations based on built-in ABAP types
"Numeric types
TYPES te_i TYPE i.
TYPES te_int8 TYPE int8.
TYPES te_decfl16 TYPE decfloat16.
TYPES te_decfl34 TYPE decfloat34.
TYPES te_f TYPE f.
TYPES te_p_l4_d2 TYPE p LENGTH 4 DECIMALS 2.
"Note: LENGTH/DECIMALS must be specified when using the types c, p, n, x
"in ABAP Objects contexts as it is the case here in the example class.
"Character-like types
"To combine TYPES statements, you can use chained statements,
"i.e. TYPES followed by a colon and then listing the type declarations separated
"by a comma.
TYPES: te_c5 TYPE c LENGTH 5,
te_n4 TYPE n LENGTH 4,
te_str TYPE string.
"Byte-like types
TYPES te_do_string TYPE x LENGTH 2.
TYPES te_xstr TYPE xstring.
"Types for date and time
TYPES te_d TYPE d.
TYPES te_t TYPE t.
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.
"**** Data type declarations based on existing types or data objects ****
"Type declaration based on an existing type visible at this location;
"all properties of the specified data type are inherited.
TYPES te_another_i TYPE te_i.
"Anticipating the data object declaration needed to demonstrate the LIKE addition
DATA do_num TYPE i.
"LIKE addition:
"Type declaration based on an existing data object visible at this location;
"all properties of the type of the specified data object are inherited.
TYPES te_from_int LIKE do_num.
"**** Data type declarations based on globally available types or data objects ****
"DDIC Types
"Note that the built-in types b and s cannot be specified for type
"declarations. However, the value range for these types can be obtained by
"referencing the built-in DDIC types INT1 and INT2. These are data elements.
"In ADT, you can check out the data elements by forward navigation (hold CTRL
"and click on the type). You can also use F2 Information (click F2 when on
"the type) to get information.
TYPES te_int1 TYPE int1.
TYPES te_int2 TYPE int2.
"Referring to types in global classes
"Also here, check out the forward navigation or F2 information for the types.
"In the example, the type exists in a global interface.
TYPES te_elem_from_itf TYPE zdemo_abap_get_data_itf=>occ_rate.
"Referring to a data object that exists in a global interface
TYPES te_dobj_from_itf LIKE zdemo_abap_objects_interface=>stat_str.
"Referring to a data object that exists in the public visibility section of
"a global class
TYPES te_dobj_from_cl LIKE zcl_demo_abap_objects=>public_string.
"Referring to a component of a DDIC table (also possible for views;
"the components have elementary types)
TYPES te_comp_ddic_tab TYPE zdemo_abap_carr-carrid.
"Type pools (ABAP program, administrated by the ABAP Dictionary; may only be
"created in standard ABAP; but is considered obsolete).
"However, the following example is accessible in ABAP for Cloud Development.
"The type pool contains the definitions of globally visible data types and
"constants. Check it out using the forward navigation and the F2 information.
TYPES te_tp TYPE abap_bool.
TYPES te_const_in_tp LIKE abap_true.
out->write( no_output ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `2) Declaring data types based on complex types` ) ).
"Structure and internal table types as examples for complex types
"Structure type, can contain any type
TYPES: BEGIN OF ts_misc_comps,
comp1 TYPE i,
comp2 TYPE string,
comp3 TYPE te_i, "Existing type
comp4 LIKE do_num, "Referring to existing data object
comp5 TYPE string_table, "Internal table type (available in DDIC)
comp6 TYPE TABLE OF zdemo_abap_carr WITH EMPTY KEY, "Internal table type (based on database table)
comp7 TYPE REF TO i, "Reference type
END OF ts_misc_comps.
"Internal table types
"Note: The examples only use the implicit STANDARD for standard tables.
"Internal table type declaration based on a local structure type
TYPES tt_local_ts TYPE TABLE OF ts_misc_comps WITH EMPTY KEY.
"Internal table type declaration based on an elementary data type
TYPES tt_int TYPE TABLE OF i.
"Referring to existing types and data objects
"Anticipating the creation of structured data objects for the LIKE addition
DATA struc_local_ts TYPE ts_misc_comps.
"Structure type creation based on an existing structured data object
TYPES ts_w_like LIKE struc_local_ts.
"Anticipating the creation of an internal table for the LIKE addition
DATA itab_local_ts TYPE TABLE OF ts_misc_comps WITH EMPTY KEY.
"Internal table type declaration based on an existing internal table
TYPES tt_w_like LIKE itab_local_ts.
"Internal table type declaration based on the existing internal table type
TYPES tt_another_type TYPE tt_w_like.
"Structured types based on an internal table's line type
TYPES ts_type_line TYPE LINE OF tt_w_like.
TYPES ts_like_line LIKE LINE OF itab_local_ts.
"Internal table typed with internal table as line type
TYPES tt_like_table LIKE TABLE OF itab_local_ts.
"Referring to global types
"Structure type based on DDIC type
"In this case, a database table is specified whose line type is used as data type
"in this type declaration. You may also use a CDS view (or classic DDIC view in
"standard ABAP) or a dedicated structured type defined in the DDIC.
TYPES ts_ddic_tab TYPE zdemo_abap_carr.
"Internal table type based on internal type that exists in a gloabl interface
TYPES tt_tab_type_from_itf TYPE zdemo_abap_get_data_itf=>carr_tab.
"Internal table types with an elementary line type based on globally available types
"Elementary table type
TYPES tt_strtab TYPE string_table.
"Elementary line type; the type is available in a global interface
TYPES tt_elem_type_from_itf TYPE TABLE OF zdemo_abap_get_data_itf=>occ_rate.
out->write( no_output ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `3) Declaring reference types` ) ).
"Declaring reference types with static types
TYPES tr_i TYPE REF TO i.
TYPES tr_str TYPE REF TO string.
TYPES tr_ddic_tab TYPE REF TO zdemo_abap_carr.
"Using the generic type data as static type
TYPES tr_data TYPE REF TO data.
"Referring to an existing reference type
TYPES tr_ref_i TYPE tr_i.
"Anticipating the creation of a data reference variable for showing
"the LIKE addition
DATA dref_i TYPE REF TO i.
"Creating a reference type based on a data reference variable
TYPES tr_like_ref_i LIKE dref_i.
"Creating a data object for the LIKE REF TO addition
DATA str TYPE string.
"Creating a reference type whose static type is inherited from the data
"type of the specified data object
TYPES tr_like_ref2str LIKE REF TO str.
"Reference table types
TYPES tr_tab_ref_i TYPE TABLE OF REF TO i.
DATA itab_str TYPE TABLE OF string.
TYPES tr_like_table_ref LIKE TABLE OF ref TO itab_str.
out->write( no_output ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `Declaring data objects` ) ).
"The following examples deal with the declaration of data ojects.
"They show how data objects can be declared locally in an ABAP program.
"Note:
"- With the exception of two additions (VALUE and READ-ONLY), the syntax
" follows the pattern as for TYPES statements.
"- A data type defined using DATA, which is not derived from an existing type,
" is available only as a property of the
" declared data object and not on its own. This kind of data type is bound to
" its data object.
"- The examples show the creation of named data objects. Anonymous data objects
" are covered below.
"- In ADT and because of the many type declarations, you may want to press F2 on
" the types to get more information.
"- The examples show a selection. For more information, check out the ABAP
" Keyword Documentation.
out->write( |4) Declaring data objects based on elementary data types\n\n| ).
"The elementary, built-in data types can be used as shown for data type
" declarations. Chained statements are also possible with DATA.
"Note that not all types as shown above are used here.
DATA: do_i TYPE i,
do_c_l5 TYPE c LENGTH 5,
do_p_l3_d2 TYPE p LENGTH 3 DECIMALS 2,
do_decfl16 TYPE decfloat16,
do_str TYPE string,
"Specifying the length in parantheses instead of using the
"LENGTH addition is not recommended
do_ctwo(2) TYPE c.
"Referring to locally declared data types
TYPES te_string TYPE string.
DATA do_another_str TYPE te_string.
"Referring to other data objects
DATA do_like_dobj LIKE do_i.
"If the length is not specified explicitly for the ABAP types c, n, p, and x,
"the standard length is defined implicitly. Check the F2 information.
DATA do_c_std TYPE c.
DATA do_p_std TYPE p.
"If neither TYPE nor LIKE is specified, a data object with the bound
"data type 'c LENGTH 1' is created.
DATA do_c.
"VALUE addition
"Start values can be set for the data objects when they are declared.
"Without the addition VALUE, data objects are filled with their type-specific
"initial values. The start value can either be specified as a literal or as a
"predefined constant.
"Note: The VALUE addition is not to be confused with the VALUE operator that
"can be used to construct the content of complex data objects as shown below.
DATA do_c_l2 TYPE c LENGTH 2 VALUE 'hi'.
DATA do_i_val TYPE i VALUE 123.
DATA do_like_val LIKE do_i VALUE 9.
"Specifying a constant (data object that cannot be changed at runtime) after
"the VALUE addition
CONSTANTS con TYPE string VALUE `abcdef`.
DATA do_val_con TYPE string VALUE con.
"VALUE IS INITIAL addition: Explicitly specifying the type-specific initial value
DATA do_i_init TYPE i VALUE IS INITIAL.
DATA do_i_like_init LIKE do_i VALUE IS INITIAL.
"Data objects can also be created in the declaration part of classes and
"interfaces. There you can use the READ-ONLY addition for data object
"declarations in the public visibility section. In doing so, an attribute
"declared using CLASS-DATA or DATA can be read from outside of the class but
"can only be changed using methods of the class or its subclasses.
"The following attribute is taken from this executable example. It shows a
"read access in a control structure. If you wanted to assign a new value to
"the attribute outside of the class, a syntax error would be displayed.
"Note that when you are in the class itself, there is no need to specify the
"class name.
"read_only_attribute = ... would be sufficient. And changing the value would
"be possible within the class, too.
"Declaration in the example:
"CLASS-DATA: read_only_attribute TYPE string VALUE `Hallo` READ-ONLY.
IF zcl_demo_abap_dtype_dobj=>read_only_attribute = `adapt read only attribute`.
...
"Since we are here in the very class of this example attribute, a changing
"of the value would be possible. And the class name can, in that case, be
"ommitted.
read_only_attribute = `changed`.
ELSE.
...
ENDIF.
out->write( no_output ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `5) Declaring structures and internal tables as examples for complex types` ) ).
"Note: See more details and examples in the ABAP Keyword Documentations and in the
"respective ABAP cheat sheets.
"Creating a structure with DATA and providing start values with the VALUE addition.
"If not specified, then the components have their type-specific initial value.
DATA: BEGIN OF a_structure,
comp1 TYPE i VALUE 1,
comp2 TYPE string VALUE `hi`,
comp3 TYPE string,
END OF a_structure.
"Creating a structure based on a global type. In this case, it is a DDIC database
"table whose line type is used. You can also use a CDS view or a dedicated structured type
"from the DDIC, for example.
DATA struc_ddic_tab TYPE zdemo_abap_carr.
"Creating a structure as a constant. Providing values is mandatory.
CONSTANTS: BEGIN OF con_struc,
comp1 TYPE i VALUE 1,
comp2 TYPE string VALUE `hallo`,
comp3 TYPE string VALUE `salut`,
END OF con_struc.
"Using the constant as start value for a structure declaration.
DATA struc_w_val LIKE con_struc VALUE con_struc.
"Declaring a structure and explicitly specifying the type-specific
"initial values of the structure components as start values.
DATA struc_init_val LIKE con_struc VALUE IS INITIAL.
"Creating internal tables ...
"Based on a globally available DDIC database table whose line type is used
DATA itab_ddic_tab TYPE TABLE OF zdemo_abap_carr WITH EMPTY KEY.
"Based on an elementary type
DATA itab_tab_i TYPE TABLE OF i.
"The table type is declared in a global interface
DATA itab_tab_type_from_itf TYPE zdemo_abap_get_data_itf=>carr_tab.
"Based on globally available DDIC internal table type; explicitly specifying as initial
DATA itab_ddic_tab_type TYPE string_table VALUE IS INITIAL.
"Based on locally available structured data object
DATA itab_like_struc LIKE TABLE OF struc_w_val WITH EMPTY KEY.
"Based on locally available internal table
DATA itab_like_another_itab LIKE itab_tab_i.
"Creating an internal table type locally
TYPES tt_ddic_tab TYPE TABLE OF zdemo_abap_fli WITH EMPTY KEY.
"... and an internal table based on it.
DATA itab_w_itab_type TYPE tt_ddic_tab.
"Creating a structure based on the line of an internal table (type)
DATA struc_from_itab_type TYPE LINE OF tt_ddic_tab.
DATA struc_like_line LIKE LINE OF itab_ddic_tab.
out->write( no_output ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `6) Declaring data reference variables` ) ).
"Declaring data reference variables types with static types
DATA dref_int TYPE REF TO i.
DATA dref_str TYPE REF TO string.
DATA dref_ddic_tab TYPE REF TO zdemo_abap_carr.
"Using the generic type data as static type
DATA dref_8_dataa TYPE REF TO data.
"Referring to an existing reference type
TYPES tr_int TYPE REF TO i.
DATA dref_tr_int TYPE tr_int.
"Creating a data reference variable based on a data reference variable
DATA dref_like LIKE dref_int.
"Creating a data object for the LIKE REF TO addition
DATA do_some_string TYPE string.
"Reference type is created whose static type is inherited from the data type of
"the specified data object
DATA dref_like_ref_str LIKE REF TO do_some_string.
"Reference tables
DATA dref_tab_i TYPE TABLE OF REF TO i.
DATA dref_tab_str LIKE TABLE OF REF TO do_some_string.
out->write( no_output ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `7) Assigning values to data objects` ) ).
"An assignment passes the content of a source to a target data object.
"Note:
"- There are conversion rules when assigning a source to a target data object
" that have different types. See further down.
"- In old ABAP code, you may see MOVE ... TO ... statements for value
" assignments. These statements are obsolete. This is not to be confused
" with MOVE-CORRESPONDING statements for complex types. They are not obsolete.
"As mentioned, a start value can be directly assigned when declaring a data object.
DATA some_int TYPE i VALUE 123.
"Assignment using the assignement operator =
"The source of the assigment on the right side (also known as general
"expressions posisiton) can be specified with many things.
"Single data object as source of the assignment
"In the following cases, the literal and data object have the exact type
"as the data object on the left side.
some_int = 456.
DATA num TYPE i.
num = some_int.
DATA str_a1 TYPE string VALUE `hallo`.
DATA str_a2 TYPE string.
str_a2 = str_a1.
"Functional method as source of the assignment
"In the following example, the method get_next of the class cl_abap_random_int
"returns an integer. Check the F2 information for get_next (return value of type i).
"A random integer that is in the specified value range is assigned to the data object
"on the left side.
num = cl_abap_random_int=>create(
seed = cl_abap_random=>seed( )
min = 1
max = 10 )->get_next( ).
"Built-in functions as source of the assignment
"There are plenty of functions available.
"Built-in numeric function
"The following built-in function calculates 2 to the power of 4. The
"result is assigned to the data object on the left side.
num = ipow( base = 2 exp = 4 ).
"Built-in string function
"The following function transforms the specified data object to upper case letters.
"The result is assigned to the data object on the left side.
str_a1 = to_upper( str_a2 ).
"Constructor expressions as source of the assignment
"There are various options and expressions available (with many additions).
"Check the ABAP Keyword Documentation and the cheat sheet. Here, taking the VALUE
"operator as an example. This operator is very handy especially for complex types.
"Creating a structure
DATA some_struc TYPE zdemo_abap_carr.
"Assignment using the VALUE operator
"Note the # character that stands for the type. Here, the structure type can be
"derived from the context. Hence, the explicit name can but need not be specified.
some_struc = VALUE #( carrid = 'XY' carrname = 'XY Airways' ).
"Creating an internal table and assigning values
"Note that components that are not specified and assigned a value retain their
"type-specific ininial value.
DATA some_itab TYPE TABLE OF zdemo_abap_carr WITH EMPTY KEY.
some_itab = VALUE #( ( carrid = 'XY' carrname = 'XY Airways' )
( carrid = 'ZZ' carrname = 'ZZ Airlines' ) ).
"Table expressions as source of the assignment
"A structure is assigned an internal table line
some_struc = some_itab[ 2 ].
"Calculation expressions as source of the assignment
"Arithmetic expressions
num = 1 + 2.
"A calculation assignment as follows
num += 1.
"is the short form of
num = num + 1.
"Syntax options: +=, -=, *= and /=
"String expressions as source of the assignment
str_a2 = str_a1 && ` blabla`. "Strings are appended using the && operator
str_a2 = |{ str_a1 } some more bla.|. "String templates
"Note: Data objects are specified in curly brackets. The content is converted to type string.
" It must be convertible to type string.
"An elementary data object is assigned a component of a specific table line using
"a table expression. Note: In the following case, the types of source and target are not
"the same (type c versus type string). As shown further down, such an assignment can lead
"to unexpected or undesired results. Type-dependent conversions are made in accordance
"with the conversion rules.
str_a2 = some_itab[ 2 ]-carrname.
out->write( no_output ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `8) Creating data objects by inline declaration` ) ).
"The declaration operator DATA can be specified in any designated declaration position.
"The data type of the variable is determined by the operand type. It must be possible
"to derive this type completely statically.
"Note:
"- The FINAL declaration operator is available for creating
" immutable variables as shon below.
"- As shown in the previous section, there are many options for what can be placed on
" the right side.
"Data object declarations and assignements as shown above.
DATA str_b1 TYPE string VALUE `abc`.
DATA str_b2 TYPE string.
str_b2 = `def`.
"Using a declaration expression with the declaration operator DATA, a data object
"can be declared inline.
"The data type of the declared variable is determined by the right side.
"In doing so, a data object is assigned a value in one go.
"In the following case, it is a string literal with backquotes on the right side.
"The data type is derived and, hence, the data object is of type string.
DATA(str_b3) = `ghi`.
"In the following case, it is a text field literal with quotes. Type c is derived.
"The length is derived from the number of characters.
DATA(c_l3) = 'jkl'. "type c length 3
DATA(c_l4) = 'mnop'. "type c length 4
"Note the type conversion implications when making an assignment with these two
"data objects.
c_l3 = c_l4. "c_l3: 'mno'
"Structures and internal tables
"In declaration expressions, structures and internal tables can be declared inline
"and filled with, for example, the help of the VALUE operator.
"Structured data type
TYPES: BEGIN OF ts_struc,
comp1 TYPE i,
comp2 TYPE string,
END OF ts_struc.
"In the following examples, structures are created. The structured data type is
"derived from the type specified before the parentheses.
DATA(struc_b1) = VALUE ts_struc( comp1 = 1 comp2 = `A` ).
"No components specified and values assigned means an initial structure.
"This syntax is also possible for declaring data objects with elementary types
"and explicitly specifiying initial values, but only for initial values.
"See the CONV operator below.
DATA(struc_b2) = VALUE ts_struc( ).
DATA(elem_init) = VALUE i( ).
"Note that components that are not specified and assigned a value remain initial.
DATA(struc_b3) = VALUE zdemo_abap_carr( carrid = 'AB' carrname = 'AB Airlines' ).
"An entire structure is assigned.
DATA(struc_b4) = struc_b1.
"Note: When the structure has already been declared, and you want to assign values,
"you can use the VALUE operator followed by the # character instead of the explicit
"type name. In that case, it is possible to derive the type from the context.
struc_b4 = VALUE #( comp1 = 2 comp2 = `b` ).
"Internal tables
"The internal table type is specified before the parentheses after the VALUE operator.
"The following example uses a table type that is globally available in the DDIC.
DATA(itab_b1) = VALUE string_table( ( `a` )
( `b` )
( `c` ) ).
"Using a local internal table type
TYPES tt_b1 TYPE TABLE OF ts_struc WITH EMPTY KEY.
DATA(itab_b2) = VALUE tt_b1( ( comp1 = 1 comp2 = `a` )
( comp1 = 2 comp2 = `b` )
( comp1 = 3 comp2 = `c` ) ).
"In the context of other ABAP statements such as LOOP, READ TABLE or ABAP SQL
"SELECT statements, inline declarations are useful for creating target variables with
"appropriate data types in place. This includes data reference variables and field
"symbols. Field symbols are not covered below.
"A work area/structure to hold the current internal table line is created inline.
LOOP AT itab_b2 INTO DATA(wa_b1).
wa_b1-comp1 = 12345.
...
ENDLOOP.
"Using the REFERENCE addition, a data reference variable can be created inline.
LOOP AT itab_b2 REFERENCE INTO DATA(wa_ref_b1).
wa_ref_b1->comp1 = 67890.
...
ENDLOOP.
"A structure to hold the internal table line read is created inline.
READ TABLE itab_b2 INTO DATA(wa_b2) INDEX 2.
"Data reference variable
READ TABLE itab_b2 REFERENCE INTO DATA(wa_ref_b2) INDEX 2.
"ABAP SQL statements
"A structure as target data object is created inline.
SELECT SINGLE * FROM zdemo_abap_carr INTO @DATA(struc_b5).
"NEW addition of the INTO clause creates a data reference variable
SELECT SINGLE * FROM zdemo_abap_carr INTO NEW @DATA(struc_ref).
"Internal table as target data object is created inline.
SELECT * FROM zdemo_abap_carr INTO TABLE @DATA(itab_b3).
"NEW addition
SELECT * FROM zdemo_abap_carr INTO TABLE NEW @DATA(itab_ref).
out->write( no_output ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `9) Assigning references to data reference variables` ) ).
"Note:
"- As is true for other data object and types, there are special assignment rules
" for data reference variables. Check out the ABAP Keyword Documentation.
"- An initial reference variable contains the null reference, which does not point
" to any objects. This means that it does not have a data type or a class as
" a dynamic type.
"Declaring data reference variables with static types
"At this stage, initial reference variables contain null references.
DATA dref_1_i TYPE REF TO i.
DATA dref_2_str TYPE REF TO string.
"Generic type as static type
DATA dref_3_data TYPE REF TO data.
"References in data reference variables can point to existing data objects.
"For assigning the reference, you can use the REF operator.
"There is also an ABAP statement available doing the same: GET REFERENCE.
"It should not be used anymore, especially in ABAP for Cloud development.
"Creating data objects to refer to and providing a start value
DATA do_number TYPE i VALUE 987.
DATA do_string TYPE string VALUE `abc`.
"After the assignment, the data reference variable points to the values.
"The data type is derived (dynamic type).
dref_1_i = REF #( do_number ). "Dynamic type is the same as the static type in this case
dref_2_str = REF #( do_string ). "Dynamic type is the same as the static type in this case
"Dynamic types of the followig examples are more specific than static type,
"which is a generic type in this case.
dref_3_data = REF #( do_number ).
dref_3_data = REF #( do_string ).
"Note: Table expressions can be also specified within the parentheses.
"Inline declarations are also possible to create data reference variables
"and assigning values in one go. Check the F2 information.
DATA(dref_4_data) = dref_3_data.
DATA(dref_5_str) = REF #( `hi` ).
DATA(dref_6_i) = REF #( do_number ).
"Assignments between two data reference variables mean that references are copied.
"The concepts of upcast and downcast enter the picture here.
"Two different assignment operators are used, as well as the casting operator CAST.
"Upcast is possible for elementary data types
"- The static type of the target variable is more general or identical to the static
" type of the source variable.
"- Assignment operator used: =
"- Note that the operators for downcasts can also be used explicitly here, but it is
" usually not needed.
"- In this example, elementary data types are covered. An upcast works ...
" - if the data types have identical type properties (i.e. the built-in type match
" as well as length and decimal places).
" - the static type of the source variable is completely typed, and the static type
" of the target variable is generic.
"The following upcasts work. Both point to data objects of type i or string.
dref_1_i = dref_6_i.
"The source on the right side is completely typed (type i),
"the target on the left side is a generic type (type data).
dref_3_data = dref_1_i.
"Downcasts
"- The static type of the target variable is more specific than the static type of the
" source variable.
"- The assignability is not checked until runtime.
"- Must always be performed explicitly using the casting operator ?= or the more
" modern casting operator CAST.
"The following example would result in a syntax error due to type incompatibility.
"dref_1_i = dref_3_data.
"In the following example, the source has a generic static type (data). The target type
"has a more specific type (type i).
"To suppress the syntax error, the CAST operator is needed.
"Note:
"- The assignability is still not checked. This is done at runtime.
" In this example, it works since the dynamic type of the source is also of type i.
"- An advantage of the CAST operator compared to ?= is that the operator enables downcasts
" in operand positions, which helps reduce helper variables.
dref_1_i = CAST #( dref_3_data ).
"If not caught, the following would result in a runtime error.
"dref_3_data points to a data object of type i, the static type of dref_2_str is string.
"So, the downcast does not work.
TRY.
dref_2_str = CAST #( dref_3_data ).
CATCH cx_sy_move_cast_error INTO DATA(e).
out->write( data = e->get_text( ) name = `e->get_text( )` ).
ENDTRY.
"Old syntax using the ?= operator
dref_1_i ?= dref_3_data.
"For upcasts, the operators can be used, too, but they are usually not necessary.
"So, an assignment as follows is possible but not needed. Only using = is sufficient.
dref_1_i = CAST #( dref_6_i ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `10) Creating anonymous data objects` ) ).
"Anonymous data objects are a topic related to data reference variables.
"These data objects are unnamed data objects.
"Most of the data objects above are named data objects, i.e. they can be addressed
"by a dedicated name.
"Unnamed data objects are literals and anonymous data objects. Anonymous data objects
"can be addressed using data reference variables.
"Note: Unlike data objects created with the statement DATA, anonymous data objects
"are created at runtime. Data objects declared using DATA are created when the
"program is loaded.
"Options to create anonymous data objects
"- CREATE DATA statements
"- Using the instance operator NEW
"- Addition NEW of the INTO clause in ABAP SQL SELECT statements
"CREATE DATA statements
"Note that there are many additions available. The examples show a selection.
"Creating an anonymous data object with an implicit type.
"If neither of the additions TYPE or LIKE are specified, the data reference variable
"must be completely typed.