5
Generated by Jive on 2016-05-25+02:00 1 ABAP Development: ABAP News for Release 7.40 - Constructor Operators COND and SWITCH Posted by Horst Keller  28 May, 2013 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 expli cit 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!  

Abap News for Release 740 Constructor Operators Cond and Switch

Embed Size (px)

Citation preview

Page 1: Abap News for Release 740 Constructor Operators Cond and Switch

7/25/2019 Abap News for Release 740 Constructor Operators Cond and Switch

http://slidepdf.com/reader/full/abap-news-for-release-740-constructor-operators-cond-and-switch 1/5

Generated by Jive on 2016-05-25+02:00

1

ABAP Development: ABAP News for Release7.40 - Constructor Operators COND andSWITCH

Posted by Horst Keller  28 May, 2013 

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!

 

Page 2: Abap News for Release 740 Constructor Operators Cond and Switch

7/25/2019 Abap News for Release 740 Constructor Operators Cond and Switch

http://slidepdf.com/reader/full/abap-news-for-release-740-constructor-operators-cond-and-switch 2/5

ABAP Development: ABAP News for Release 7.40 - Constructor Operators COND and SWITCH

Generated by Jive on 2016-05-25+02:00

2

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) =

Page 3: Abap News for Release 740 Constructor Operators Cond and Switch

7/25/2019 Abap News for Release 740 Constructor Operators Cond and Switch

http://slidepdf.com/reader/full/abap-news-for-release-740-constructor-operators-cond-and-switch 3/5

ABAP Development: ABAP News for Release 7.40 - Constructor Operators COND and SWITCH

Generated by Jive on 2016-05-25+02:00

3

  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?

7585 Views Tags: abap

Michael Calekta in response to Horst Keller  on page 3 

17 Feb, 2016 4:14 PM

I see - so it's time to move to Eclipse, I guess. Well, that will be rather a hard thing to get my company to take

this step...

Horst Keller in response to Michael Calekta  on page 3 

17 Feb, 2016 4:01 PM

Phew ...

 

Not much investment into classical SE11 any more and let's see how the Dictionary Tools in ADT will get

along. Up to now (7.50) we have a form based editor for data elements and a source code based editor for

structures in ADT.

Michael Calekta in response to Horst Keller  on page 3 

17 Feb, 2016 3:57 PMOkay - understood.

The word "currently" gives hope, that it might be possible in future versions, right?

Horst Keller

17 Feb, 2016 3:53 PM

The vanished question was:

 

Question: How do I define a global Table-Type in the DDIC as Standard-Table with empty

key? There's no fitting option in the tab for the primary-key definition. Unless it's the "Notspecified"-option, but not specified is in my understanding different from empty, is it not?

 

Answer: You can't.

 

"It is not currently possible to specify an empty primary table key explicitly in ABAP Dictionary"

 

If possible, define your table types in a class or an interface.

Page 4: Abap News for Release 740 Constructor Operators Cond and Switch

7/25/2019 Abap News for Release 740 Constructor Operators Cond and Switch

http://slidepdf.com/reader/full/abap-news-for-release-740-constructor-operators-cond-and-switch 4/5

ABAP Development: ABAP News for Release 7.40 - Constructor Operators COND and SWITCH

Generated by Jive on 2016-05-25+02:00

4

Horst Keller in response to Tom Dhooghe  on page 4 

8 Apr, 2015 3:38 PM

Latest versions of SAP GUI and ADT Editors do it right.

Tom Dhooghe

8 Apr, 2015 2:25 PMThe COND has been very useful as a sort of ternary operator to streamline code that would otherwise look very

convoluted.

 

Sadly, it seems that the editor's syntax highlighting has not been updated for this notation and it will mark parts

of the statement in red, indicating they are invalid even though it compiles and executes fine. Are there plans to

update the syntax highlighter to support these statements? (Tested in 7.4 SP7 and SP9)

Horst Keller in response to Paul Hardy  on page 4 

29 May, 2013 10:13 AM

No support for this in SAP GUI but in (at least my) Eclipse, crtl+F1 on class=>meth( ) and it will create the

method for you. 

Nevertheless, from my learning of ABAP long time ago I remember some very misleading effects from this. In

old ABAP trainings they've explained subroutines (FORMs) coming from PERFORM. First write PERFORM.

double click, implement. Problem was that people did not understand that the FORM and its parameter

interface are the important part and that PERFORM is only its user. There were big problems in explaining the

modes forparameter passing because everything was based on PERFORM and not on FORM where the real

definition lies. But let's expect, that nowadays developers know what they are doing ...

Paul Hardy

29 May, 2013 9:19 AM

While we are talking about local classes and methods, are there any plans in 740 or beyond, that when you re

writing your code and call a method of a class and it does not exists, you get the "method does not exists - do

you want to create it?" and it creates a skeleton implementation and defintion, which is the case for PERFORM

routines.

 

This would speed up test driven development enormously. Sometimes people wonder why programmers are

slow to adopt OO, it is little niggly things like this that make writing methods more work than form routines.

People are lazy so they take the easy option.

 

I was told this is possible in ABAP in Eclipse.....

Horst Keller in response to Suhas Saha  on page 4 

29 May, 2013 7:40 AM

The exception is not thrown inside meth but in the context where meth is called. This context must have a

raising clause which is not shown here.

Suhas Saha

28 May, 2013 10:40 PM

Page 5: Abap News for Release 740 Constructor Operators Cond and Switch

7/25/2019 Abap News for Release 740 Constructor Operators Cond and Switch

http://slidepdf.com/reader/full/abap-news-for-release-740-constructor-operators-cond-and-switch 5/5

ABAP Development: ABAP News for Release 7.40 - Constructor Operators COND and SWITCH

Generated by Jive on 2016-05-25+02:00

5

Exception cx_langu_not_supported is not defined in the signature of meth( )

How to handle it then?