Strange behaviour using string templates and new COND syntax

二次信任 提交于 2019-11-27 08:21:39

问题


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.



来源:https://stackoverflow.com/questions/35994332/strange-behaviour-using-string-templates-and-new-cond-syntax

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!