Ansicht
Dokumentation

ABAPASSIGN_CASTING - ASSIGN CASTING

ABAPASSIGN_CASTING - ASSIGN CASTING

General Material Data   Addresses (Business Address Services)  
This documentation is copyright by SAP AG.
SAP E-Book

ASSIGN, casting_spec

Short Reference



... { }
  $| ${ CASTING ${ ${ $}
              $| ${TYPE type$|(name)$}
              $| ${LIKE dobj$}
              $| ${$[TYPE p$] DECIMALS dec$}
              $| ${TYPE HANDLE handle$} $} $}
  $| ${ obsolete_casting $} ...

Alternatives:

1. ... ${ $}

2. ... CASTING ...

Effect

The specification casting_spec defines the data type used to handle the memory area mem_area assigned to the field symbol when a statement contains the field symbol in an operand position. Either the addition CASTING can be specified or nothing at all. Outside of classes, the obsolete variants obsolete_casting are also possible.

The following restrictions apply:

  • In assignments of table expressions, only the first alternative can be used without further additions.
  • The CASTING addition cannot be used for enumerated types. This means that the assigned memory area must not be an enumerated object and the specified data type must not be an enumerated type.

Alternative 1

... ${ $}


Effect

If nothing is specified for casting_spec, the field symbol inherits the data type of the data object used in mem_area and the assigned memory area is handled accordingly. This data type must match the typing of the field symbol.

Example

After the first assignment the field symbol has type c of length 3 and after the second assignment the field symbol has type string. The statement DESCRIBE FIELD returns the corresponding values C 3 and g.

Alternative 2

... CASTING ...


Additions

1. ... ${ $}

2. ... TYPE type$|(name)

3. ... LIKE dobj

4. ... $[TYPE p$] DECIMALS dec

5. ... TYPE HANDLE handle

Effect

If the addition CASTING is used in casting_spec, the memory area is handled as if it had the type specified by CASTING. If CASTING is specified, the field symbol cannot be typed using the obsolete addition STRUCTURE of the statement FIELD-SYMBOLS.

Casting can either take place implicitly using the typing of the field symbol or explicitly using one of the additions TYPE, LIKE, or DECIMALS. In explicit castings, the field symbol can only be typed generically, not completely.

Notes

  • If the data type determined by CASTING is deep or if deep data objects are stored in the assigned memory area, the deep components must appear with exactly the same type and position in the assigned memory area. In particular, this means that individual reference variables can be assigned to only one field symbol that is typed as a reference variable by the same static type.
  • The static check of the statement ASSIGN using the addition CASTING is performed so that all errors are identified, regardless of the system or platform on which the check is run. A runtime check only checks the current system or the current platform. These checks also always take place if the field symbol <fs> is typed with the obsolete addition STRUCTURE of the statement FIELD-SYMBOLS.
  • If the addition CASTING is used, the result of the statement ASSIGN may be platform-dependent. The internal byte order of characters can, for example, be platform-dependent, which becomes apparent in a cast to a byte-like data type.
  • The memory area mem_area must meet the alignment requirements of the data type specified by the casting. When flat elementary data types are specified, for example, the memory address mem_area must be divisible as follows:
  • By 2 for the character-like data types c and n and the date/time types d and t.

  • By 4 for the numeric data type i.

  • By 8 for the numeric data types int8, f, and decfloat16.

  • By 8 for the time stamp type utclong.

  • By 16 for the numeric data type decfloat34.

Addition 1

... ${ $}

Effect

If the addition CASTING is specified without further additions, the assigned memory area is cast to the type of the field symbol. The field symbol must be either completely typed or with one of the generic built-in ABAP types c, n, p, or x.

Casting of the integer 333 as a byte field. Depending on the byte order, the output is either 4D01 or 014D.

Casting of the integer 333 as a byte field. Depending on the byte order, the result is either 4D01 or 014D.

Addition 2

... TYPE type$|(name)

Effect

Explicit specification of a data type after TYPE. The name of the type can be specified as follows:

  • type
Direct static specification as type.
  • (name)
Specification as the content of a character-like data object name that contains the name of a data type in uppercase letters when the statement is executed. The following can be specified for name:
  • Literal or constants

If the data object name is specified as a character literal or as a constant, it can be evaluated statically, and the specified type is recognized as the used object.
  • Variable

If the data object name is specified as a variable, it is specified only dynamically, and the content is not evaluated statically.
When the statement is executed, name is not evaluated until runtime in both cases.

The assigned memory area is cast to the specified type. The data type specified after TYPE cannot be generic, apart from the built-in ABAP types c, n, p, and x. Furthermore, table categories and REF TO cannot be specified.

The field symbol must be typed generically rather than in full. The specified data type must match the generic typing of the field symbol, that is, the casting can specialize the generic typing, but not generalize it.

If a generic character-like type c or n is specified after TYPE, the length of the assigned memory area must be a multiple of the length of a character in the memory when the statement is executed.

Example

Casting of a time field to a structured type with three components.

In the following example, one of the two ASSIGN statements produces a runtime error because the alignment requirement for the type c is not met. Which of the statements produces the runtime error is not generally defined and depends on the preceding declarations.

Addition 3

... LIKE dobj

Effect

The following can be specified after LIKE:

  • A data object dobj according to the rules for TYPES ...  LIKE. The assigned memory area is cast to the data type of the data object.
  • A generically typed field symbol.
  • If a memory area is assigned to the field symbol, the data type used to handle the memory area is the object of the cast.

  • If no memory area is assigned to the field symbol, a standard type resulting from the following rules is used:

any, c, clike, csequence, data, and simple produce c with length 1.
decfloat produces decfloat34.
n produces n with length 1.
numeric and p produce p with length 8 and no decimal places.
x and xsequence produce x of the length 1.
Generic table types raise an exception of the class CX_SY_ASSIGN_CAST_ILLEGAL_CAST.
  • A generically typed formal parameter.
  • If an actual parameter is assigned to the formal parameter, the data type of this parameter is the object of the cast.

The field symbol must be typed generically rather than in full. The specified data type must match the generic typing of the field symbol, that is, the casting can specialize the generic typing, but not generalize it.

Notes

  • LIKE can be used to refer to the data objects in its own program, and also to the public attributes of global classes.
  • The standard type for generically typed field symbols specified after CASTING LIKE differs slightly from the standard type for generic field symbols and formal parameters (length 1 not 4 if any and data are used and no standard type for generic table types).
  • If a generically typed field symbol is specified after CASTING LIKE, a memory area should be assigned to it when the statement is executed.

Example

Casting of a structure with three components to a time field.

Addition 4

... $[TYPE p$] DECIMALS dec

Effect

A numeric data object dec must be specified after DECIMALS. The assigned memory area is cast to the data type p, where the number of decimal places is determined by the content of dec. The number of decimal places must not exceed the total number of places. TYPE does not need to be specified for DECIMALS. If TYPE is used, only the data type p, which is used anyway, can be specified.

The field symbol must be typed generically rather than in full. The specified data type must match the generic typing of the field symbol, that is, the casting can specialize the generic typing, but not generalize it.

The calculation of the quotient from the packed number pack and the field symbol pack> demonstrates the effect of casting with the addition DECIMALS. Factors between 10 and 100,000,000 are determined. When using pack> in operand positions, a different value is used than when using pack.

The calculation of the quotient from the packed number pack and the field symbol pack> demonstrates the effect of casting with the addition DECIMALS. Factors between 10 and 100,000,000 are determined. When using pack> in operand positions, a different value is used than when using pack.

Addition 5

... TYPE HANDLE handle

Effect

After TYPE HANDLE, a reference variable handle of the static type of the class CL_ABAP_DATADESCR or its subclasses is specified that points to a type description object of the RTTS. The assigned memory area is cast to the type of the field symbol.

The field symbol must be typed generically rather than in full. The specified data type must match the generic typing of the field symbol, that is, the casting can specialize the generic typing, but not generalize it.

The addition CASTING TYPE HANDLE cannot be used with the addition RANGE.

Note

The type description object may have been created using the RTTS methods on existing data objects, or using the definition of a new data type.

Example

Generation of a type description object for a structure and using it to cast a date field.






BAL_S_LOG - Application Log: Log header data   ABAP Short Reference  
This documentation is copyright by SAP AG.

Length: 18478 Date: 20240426 Time: 114816     sap01-206 ( 233 ms )