Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
horst_keller
Product and Topic Expert
Product and Topic Expert


Predicative Method Calls

For quite some time the lack of a real boolean type in ABAP led to a lot of discussions inside and outside of SAP and especially in SCN. People kept asking, why can I not write

IF meth( ... ).

  ...

ENDIF.

The answer always was: Because you have to have a boolean type behind IF and no method can return a boolean type.

But in fact, the solution was absolutely simple and - tatarata! - is made availabe in Release 7.40, SP08.

You can write the so called predicative method call

... meth( ) ...

as a short form of the relational expression

... meth( ) IS NOT INITIAL ...

now and that's already all! Of course,  methods that return a value of type ABAP_BOOL are especially suited for that purpose. We call those predicate methods now, but it's not a prerequisite to use them.

So you can place simple functional method calls everywhere, where logical expressions are allowed: behind  IF, CHECK, ASSERT, COND, SWITCH, ...

The documentation shows some examples:

IF cl_abap_demo_services=>is_production_system( ).

  cl_demo_output=>display(

     'This demo cannot be executed in a production system' ).

  LEAVE PROGRAM.

ENDIF.

Here, a predicative method is called that returns ABAP_TRUE or ABAP_FALSE.

COND string( WHEN cl_demo_spfli=>get_spfli( to_upper( carrier ) )

               THEN `Filled`

               ELSE `Not filled` )

Here, a normal method is called and the result is true, if the table type return value contains lines.

Why didn't we enable this long before?

New Boolean Function

Did you ever stumble over that one?

IF boolc( 1 = 2 ) = abap_false.
  cl_demo_output=>display_text( 'yes' ).
ELSE.
  cl_demo_output=>display_text( 'no' ).
ENDIF.

The relational expression boolc( 1 = 2 ) = abap_false is false! Why? Because boolc despite its name does not return c but string and the comparison rules for c and string blah, blah, blah ...

Now, with Release 7.40, SP08, we have an new Boolean function.

IF xsdbool( 1 = 2 ) = abap_false.

  cl_demo_output=>display_text( 'yes' ).

ELSE.

  cl_demo_output=>display_text( 'no' ).

ENDIF.

The relational expression xsdbool( 1 = 2 ) = abap_false is true, because xsdbool returns type XSDBOOLEAN from the ABAP Dictionary that is - yes, you guess it -  c of length 1. For the experts among you, XSDBOOLEAN is normally used for special mappings in XML-transformations and was reused here for quite another purpose. And that's were the funny name xsdbool comes from.

The new Boolean function xsdbool can be used everywhere, where comparisons with type c length 1 are needed, especially comparisons with type ABAP_BOOL.But be aware of the fact, that the space that is returned by xsdbool for a false expression is cut in many operand positions, e.g. if you want to convert it with a builtin function like translate.

24 Comments