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

With Release 7.40 ABAP supports so called constructor operators. Constructor operators are used in constructor expressions to create a result that can be used at operand positions. The syntax for constructor expressions is

... operator type( ... ) ...

operator is a constructor operator. type is either the explicit name of a data type or the character #. With # the data type can be dreived from the operand position if the operand type is statically known. Inside the parentheses specific parameters can be specified.

Conditional operators COND and SWITCH

Last but not least, two nice ones, the conditionals COND and SWITCH.

  • ... COND dtype|#( WHEN log_exp1 THEN result1
                    [ WHEN log_exp2 THEN result2 ]
                    ...
                    [ ELSE resultn ] ) ...

       constructs a result of the specified type that depends on logical expressions.

  • ... SWITCH dtype|#( operand
                        WHEN const1 THEN result1
                      [ WHEN const2 THEN result2 ]
                      ...
                      [ ELSE resultn ] ) ...

constructs a result of the specified type that depends on a case differentiation.

With other words: IF and CASE as expressions in operand positions!

         

Example for COND

DATA(time) =

  COND string(

    WHEN sy-timlo < '120000' THEN

      |{ sy-timlo TIME = ISO } AM|

    WHEN sy-timlo > '120000' THEN

      |{ CONV t( sy-timlo - 12 * 3600 )

         TIME = ISO } PM|

    WHEN sy-timlo = '120000' THEN

      |High Noon|

    ELSE

      THROW cx_cant_be( ) ).

Note the THROW. Now you can raise and  throw exceptions ...

Example for SWITCH

CLASS cx_langu_not_supported DEFINITION INHERITING FROM cx_static_check.
ENDCLASS.

CLASS class DEFINITION.
  PUBLIC SECTION.
    METHODS meth IMPORTING iso_langu   TYPE string
                 RETURNING VALUE(text) TYPE string.
ENDCLASS.

CLASS class IMPLEMENTATION.
  METHOD meth.
    ...
  ENDMETHOD.
ENDCLASS.

...

  DATA(text) =
    NEW class(
      )->meth(
          SWITCH #( sy-langu
                    WHEN 'D' THEN `DE`
                    WHEN 'E' THEN `EN`
                    ELSE THROW cx_langu_not_supported( ) ) ).

Amazing, n'est-ce pas?

12 Comments