EXACT is the better CONV
EXACT is the better CONV

EXACT is the better CONV

Published at February 11, 2025by Jörg Brandeis

The following texts were partially or completely generated with the help of generative AI models.

With the CONV constructor operator, we can conveniently adjust the data type of a parameter when calling a method. However, this bypasses ABAP's type checking. The EXACT constructor operator is often the better choice, because it additionally verifies that no data is lost.

The challenge

A typical problem when calling a method is mismatched data types. In contrast to function modules, the data type is checked at design time, and the class cannot be activated if the data types of method calls do not match.

Example

Method definition

METHODS my_method IMPORTING text TYPE char8.

Call with DATS

This results in an error if we call it with a date of data type DATS, because the data type does not match exactly:

"SYST-DATUM" is not type-compatible with formal parameter "TEXT"

The second error is not relevant here. It occurs because we are on a cloud system, where SYST-DATUM may no longer be used at all.

The classic solution with a helper variable

If the data type does not match, we simply create a matching variable, fill it with the data, and pass it to the method. We take advantage of the fact that in ABAP you can mix apples with oranges in an assignment. And as long as the contents are compatible, this works just fine.

  METHOD demo_conv.
    data lv_text type char8. 
    lv_text = sy-datum. 
    my_method( text =  lv_text ).
  ENDMETHOD.

We've done this a thousand times before. But it's not very elegant.

The modern solution with CONV - more elegant but just as imprecise

With the CONV constructor operator, we can make the data type match. The # automatically uses the appropriate target data type from the method signature.

  METHOD demo_conv.
    my_method( text =  CONV #( lv_text ) ).
  ENDMETHOD.

The behavior corresponds exactly to that with a helper variable.

The problem of data loss

Data loss can occur if the parameter's data type cannot hold all the data.

For example, if some classes still define the data type for the material as CHAR(18) while other places work with CHAR(40), then the data types do not match. You can adjust this with a suitable helper variable. But if at some point a material with more than 18 characters exists in the system, it is simply truncated. The resulting problems can then appear in a completely different place from where the cause originated.

What happens when data loss occurs during an assignment.

The variant with EXACT - elegant and safe

The EXACT operator can also make the data type match. In doing so, it ensures that the target data type can actually hold the data in full. If that is not the case, the ABAP runtime reacts with an exception of type CX_SY_MOVE_CAST_ERROR.

If we are sure that the data type matches, then EXACT involves no extra effort. But if we were mistaken, data is not simply truncated and then processed with incorrect data. Instead, we get a dump and immediately know what problem we have.

  METHOD demo_conv.
    my_method( text =  EXACT #( lv_text ) ).
  ENDMETHOD.

As an alternative to the dump, you can of course also catch the exception. But you will only do that if you actually anticipate this situation.

Conclusion

Modern ABAP allows us to avoid unnecessary helper variables with the concept of expressions. The CONV operator is very handy for this. The EXACT operator can do even more: it prevents accidental data loss.

More articles

New!
New!
New!