Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cleanest way to flip a Boolean

Tags:

boolean

abap

Are there any existing methods or function modules that flip boolean values efficiently?

I've come up with a simple implementation should I have to define my own utility method, but I'm wondering if this is the most efficient approach:

IF iv_bool = abap_true.
    rt_bool = abap_false.
ELSEIF iv_bool = abap_false.
    rt_bool = abap_true.
ELSE.
    rt_bool = abap_undefined.
ENDIF.

EDIT: As mentioned by Smigs, this implementation flips three-valued booleans or "trileans"

like image 333
Lilienthal Avatar asked Apr 24 '14 10:04

Lilienthal


Video Answer


1 Answers

rt_bool = boolc( iv_bool <> abap_true ).

will flip a boolean. However, it wouldn't deal with abap_undefined.

From 740 SP08 onwards, you can use xsdbool( ) instead of boolc( ) to achieve the same result. There is no difference for the example given, but xsdbool( ) is safer when using in comparisons

like image 108
Smigs Avatar answered Oct 04 '22 06:10

Smigs