GUIDELINE 6.14
Using Conversion Rules
ABAP_BACKGROUND
ABAP contains numerous conversion rules for assignments between data
objects of different data types. These rules relate to assignments
between:
Elementary data objects
Elementary data objects and
structures
Structures
Internal tables
Reference variables
Assignments between data objects of nearly every different data type are
possible. The only prohibited assignments are for data types for data
and time specifications. Almost all permitted assignments have a
corresponding conversion rule. It is especially important to have rules
for assignments between data objects of the same data type, if different
technical properties (such as length or number of decimal places) are
allowed. Lossless assignments
permit only conversions that produce valid values and where no values
are lost.
ABAP_RULE
Avoid unexpected conversion results
Only assign data objects to each other if the content corresponds to the
data type of the target field and produces an expected result. Do not
exploit every ABAP conversion rule to its full extent. Consider using
lossless assignments .
ABAP_DETAILS
The ABAP conversion rules are based on the philosophy that assignments
should be allowed between as many combinations of values as possible,
without generating exceptions. In this situation, ABAP behaves quite
differently from other programming languages. In other languages,
assignments between different data types are usually handled much more
strictly and special conversion routines or explicit casting for
specific requested conversions are used.
Although it is convenient to be able to readily assign all possible data
objects to each other, there are also disadvantages, such as the
generation of invalid values .
Another example is implicit casting , which
occurs when assignments are made between elementary data objects and
structures, or between incompatible structures.
Even if no invalid values are generated, problems still can occur. If
valid target values are generated from invalid source values, this does
not necessarily meet the expectations of the reader and it can make
program maintenance considerably difficult. One example of this is the
handling of invalid content in the source field in assignments from a
character-like type to a byte-like type. Instead of exiting the
assignment with an exception, hexadecimal zeros are passed from the
first invalid character.
The only solution here are lossless
assignments with the operator
EXACT , which produce an exception in these cases. Even though
this is a bit late in the day, the behavior of an assignment with the
operator EXACT could be regarded as the normal, expected
behavior. Other unexpected behaviors represent an implementation of
special rules, which is actually the standard behavior in ABAP.
Bad example
Anyone who is familiar with all the details of the ABAP conversion rules
would probably expect an exception when the text in the following source
code is assigned to the numeric text. However, only the digits of the
text are respected. Therefore, the target field is given the value
"00000043" instead of the value "00000007", which might also be
expected.
DATA text TYPE string,
num_text TYPE n LENGTH 8.
...
text = '4 Apples + 3 Oranges'.
...
num_text = text.
#Good example
This issue is corrected in the source code below. The
EXACT operator is used,
which triggers an exception.
...
text = '4 Apples + 3 Oranges'.
...
TRY.
num_text = EXACT #( text ).
CATCH cx_sy_conversion_error.
...
ENDTRY.
Documentation extract taken from SAP system, � Copyright SAP AG. All rights reserved