Below code snippet shows the difference between a string of initial state and a string of length 1 with blank value masquerading as abap_false boolean value.
As of ABAP release 740 SP08, xsdbool function can be used to get rid of such ambiguity.
DATA(lv_cstring_false) = boolc( 1 LT 0 ).
IF lv_cstring_false EQ abap_false.
BREAK-POINT. "not called
ENDIF.
DATA(lv_xsdbool_false) = xsdbool( 1 LT 0 ).
IF lv_xsdbool_false EQ abap_false.
BREAK-POINT. "called
ENDIF.
IF lv_cstring_false IS INITIAL.
BREAK-POINT. "called
ENDIF.
Inbuilt functions help the developer to write more compact code and get rid of helper variables.
Example taken from keyword documentation. Instead of declaring a boolean variable and using IF-ELSE-ENDIF block to fill it based on contents of sy-batch, inbuilt function can be used.
PARAMETERS word TYPE c length 30.
DATA result_tab TYPE cl_abap_docu=>search_results.
cl_abap_docu=>start(
EXPORTING word = word
no_dialog = boolc( sy-batch IS NOT INITIAL )
IMPORTING search_results = result_tab ).
If specific methods are expecting values like Y,N instead of X,space, translate function can be used inline.
DATA lv TYPE string.
lv = boolc( 1 GT 2 ).
DATA(lv_flag) = translate( val = lv from = ' X' to = 'NY' ).
WRITE / lv_flag.
lv = boolc( 1 LT 2 ).
lv_flag = translate( val = lv from = ' X' to = 'NY' ).
WRITE / lv_flag.
NOTE: abap_false loses its blank when converted to the type string. This is heavy.
No comments:
Post a Comment