Typed Data References - Overview

Using Typed Data References

As far as fully generic references are concerned, you can edit the data objects created only if you use field symbols. In case of typed data references, however, you can directly address the components of the data object referenced and therefore use them at any operand position required.

data DREF type ref to SFLIGHT.
create data DREF.
DREF->FLDATE = DREF->FLDATE + 5.
if DREF->SEATSMAX > 10.
  write: / DREF->FLDATE, DREF->SEATSMAX.
endif.

Typing requires that the type of the actual object to which you want to define the reference is known. If this is not the case as, for example, with containers, the only way to access the object is to use the fully generic technique using field symbols.

Advantages of Typing

A great advantage provided by typed data references is that data ranges are adjusted to the actual requirements. If, for example, you do not know exactly whether a specific field is needed at all in a certain structure, you define a data reference instead of a field and then use CREATE DATA to create the corresponding data object only if the field is addressed. This way, memory requirements at runtime can be reduced considerably.

types: begin of STRUC_1,
         A type I,
         B type ref to SFLIGHT,
         C type P,
       end of STRUC_1.
data: S1 type STRUC_1.
...
if S1-A > 10.
  create data S1-B.
  S1-B->CARRID = 'LH'.
  ...
  S1-B->PAYMENTSUM = 1000.
endif.

Another advantage as compared with generic data references is that the entire data object can be directly addressed at any operand position using the operator →*. The operator →* can also be used to create reference chains as shown in the following examples:

types: begin of STRUC_2,
         X(10) type C,
         Y     type ref to STRUC_1,
         Z     type I,
       end of STRUC_2.
data:  S2 type STRUC_2,
       R2 type ref to STRUC_2.
...
S2-Y->A   = 100.
S2-Y->*-A = 200. "Same as S2-Y->A ...
S2-Y->B->FLDATE = SY-DATUM.

Type Check of Reference Parameters

You may only pass a typed reference to a generic parameter typed with reference to REF TO DATA if the parameter is an IMPORTING parameter. In particular, you cannot pass a typed reference to a CHANGING or EXPORTING parameter. This is due to the fact that the reference can be changed in the program unit called with the result that an undefined state exists. In this case, the system triggers a syntax or runtime error.

Converting Reference Parameters

As with object references, you can always assign a typed reference to a generic reference. In the opposite case, use of the operator ?= is mandatory.

data: DREF_1 type ref to data,
      DREF_2 type ref to SFLIGHT.
...
DREF_2 = DREF_1.    "Syntax error

create data DREF_1 type SFLIGHT.
DREF_2 ?= DREF_1.   "OK

create data DREF_1 type SPFLI.
DREF_2 ?= DREF_1.   "Exception

The exception in the last case can be handled between TRY ... ENDTRY.

Internal Tables

Like the addition ASSIGNING for internal tables, you can use the addition REFERENCE INTO dref to place a reference to a table line into the field dref. You can then use this reference to directly operate on the contents of the internal table. The addition REFERENCE INTO dref is allowed in the following statements: LOOP, READ, APPEND, INSERT, MODIFY, and COLLECT

As a rule, the reference is only set with this addition if the statement is executed successfully. In the LOOP statement, you must not change the reference variable in the loop. When the loop processing is finished, the reference variable points to the table line which was last edited.