Strange behaviour using string templates and new C

2020-02-07 05:18发布

问题:

I have spotted a strange behaviour of new COND syntax when used inside a string template. It is about string length defaulting. It looks like the length of the string will be defaulted always to what stands after THEN even if the condition is not met.

Check out the following piece of code!

REPORT zzz.

CLASS lcl_main DEFINITION FINAL CREATE PRIVATE.
  PUBLIC SECTION.
    CLASS-METHODS:
      main.
ENDCLASS.

CLASS lcl_main IMPLEMENTATION.
  METHOD main.
    DATA(l_bool) = abap_true.
    DATA(l_v_line) = |{ COND #( WHEN l_bool IS INITIAL THEN 'AAA' ELSE 'BBBB' ) }|.
    DATA(l_v_line2) = |{ COND #( WHEN l_bool IS INITIAL THEN 'AAA' ELSE 'BBBB' ) WIDTH = 4 }|.
    DATA(l_v_line3) = |{ COND #( WHEN l_bool IS INITIAL THEN 'AAA ' ELSE 'BBBB' ) }|.
    DATA(l_v_line4) = |{ COND #( WHEN l_bool IS NOT INITIAL THEN 'BBBB' ELSE 'AAA' ) }|.
    WRITE /: l_v_line, l_v_line2, l_v_line3, l_v_line4.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  lcl_main=>main( ).

The output

BBB
BBB·
BBBB
BBBB

The first two variables l_v_line and l_v_line2 are truncated even if the condition evaluates to false. If I put space after AAA in l_v_line3 then it is OK. Putting BBBB after THEN for l_v_line4 solves the problem.

My question is: is this behaviour documented anywhere in SAP documentation? I could not find any clues that would have led me to it.

回答1:

from ABAP documentation

The # character as a symbol for the operand type.

...

If the operand type is not fully identifiable, an operand with a statically identifiable type must be specified after the first THEN. This type is then used.



标签: sap abap