CASTING in ABAP
CASTING allows to assign value from one type to another generic type by specifying the target type during assignment. The below program shows some assignments.
DATA: lv_date TYPE d.
lv_date = sy-datum.
WRITE:/ lv_date.
//—————–//
FIELD-SYMBOLS: <fs_date1> TYPE d.
* lv_date & <fs_date1> are fully typed & compatible
* so no CASTING required, just simply ASSIGN
ASSIGN lv_date TO <fs_date1>.
WRITE:/ <fs_date1>.
//—————–//
TYPES: BEGIN OF ty_date,
yy(4),
mm(2),
dd(2),
END OF ty_date.
FIELD-SYMBOLS: <fs_date2> TYPE ty_date.
* lv_date & <fs_date2> are fully typed but not compatible
* so need CASTING but not type specification
ASSIGN lv_date TO <fs_date2> CASTING.
WRITE:/ <fs_date2>.
//—————–//
* lv_date is fully typed but <fs_date3> is generic so not compatible
* so need CASTING with exlicit type ( build in type)
FIELD-SYMBOLS: <fs_date3> TYPE any.
ASSIGN lv_date TO <fs_date3> CASTING TYPE d.
WRITE:/ <fs_date3>.
//—————–//
* lv_date if fully typed but <fs_date4> is generic so not compatible
* so need CASTING with exlicit type ( user defined type)
FIELD-SYMBOLS: <fs_date4> TYPE any.
ASSIGN lv_date TO <fs_date4> CASTING TYPE ty_date.
WRITE:/ <fs_date4>.
//—————–//
Output: