EXACT - Lossless Operator
7.31 | 7.40 | 7.54
... EXACT type( [let_exp] dobj ) ...
A constructor expression with the lossless operator
EXACT performs either a
lossless assignment or a
lossless calculation (depending on the specified
dobj) and creates a result with the data type
type. The following can be specified for
- A non-generic data type
dtype(with the exception of reference types).
#character for a data type, determined in accordance with the following hierarchy:
- If the data type required in an operand position is unique and known completely, the operand type is used. The operand type can also be generic and the current type is used at runtime.
- If the data type cannot be derived from the context, the calculation type
decfloat34is used in lossless calculations and the data type of the argument is used in lossless assignments.
The parentheses must contain precisely one unnamed argument
dobj that can be converted to the data type
dobj is a
general expression position. The content of the result is defined as follows:
- If the argument
dobjis specified as an arithmetic expression, the expression is calculated in accordance with the rules for a lossless assignment and the result (with calculation type
decfloat34) is converted to the data type
- In all other cases, the content of the result is defined by an assignment of the argument in accordance with the associated conversion rules, during which a check is performed in accordance with the rules of lossless assignments.
If data is lost in either case, the corresponding exception is raised. If the argument is compatible
with the data type
type in a lossless assignment,
EXACT does not perform any checks and a syntax check warning is produced. For
enumerated types, additional special rules apply.
let_exp can be specified before the argument to define local helper fields.
Lossless assignment. Here, the exception CX_SY_CONVERSION_ERROR is raised, because the argument contains an invalid value.
TYPES numtext TYPE n LENGTH 255. TRY. DATA(number) = EXACT numtext( '4 Apples + 2 Oranges' ). CATCH cx_sy_conversion_error INTO DATA(exc). ... ENDTRY.
Lossless assignment with generic types. The first method call produces a successful assignment; the second raises the exception CX_SY_CONVERSION_EXACT_NOT_SUP. If the assignment is replaced by p2 = EXACT #( + p1 ), a lossless calculation is produced and no exception is raised.
CLASS c1 DEFINITION. PUBLIC SECTION. CLASS-METHODS m1 IMPORTING p1 TYPE data EXPORTING p2 TYPE data. ENDCLASS. CLASS c1 IMPLEMENTATION. METHOD m1. DATA arg TYPE i. TRY. p2 = EXACT #( p1 ) ##operator. cl_demo_output=>display_data( p2 ). CATCH cx_sy_conversion_exact_not_sup cx_sy_conversion_error INTO DATA(err). cl_demo_output=>display_text( err->get_text( ) ). ENDTRY. ENDMETHOD. ENDCLASS. DATA: date TYPE d, text TYPE string. START-OF-SELECTION. c1=>m1( EXPORTING p1 = sy-timlo IMPORTING p2 = text ). c1=>m1( EXPORTING p1 = sy-timlo IMPORTING p2 = date ).
Lossless calculation. Here, the exception CX_SY_CONVERSION_ROUNDING is raised, because the calculation
is not lossless. The rounded result is assigned to the inline declared variable
TRY. DATA(exact_result) = EXACT #( 3 * ( 1 / 3 ) ). CATCH cx_sy_conversion_rounding INTO DATA(exc). DATA(rounded_result) = exc->value. ENDTRY.
Cause: Invalid combination of types or lengths.