Abap News for Release 740 Constructor Operator New

Embed Size (px)

Citation preview

  • 7/25/2019 Abap News for Release 740 Constructor Operator New

    1/9

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

    1

    ABAP Development: ABAP News for Release7.40 - Constructor Operator NEW

    Posted by Horst Keller24 May, 2013With 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.

    Instantiation Operator NEW

    The instantiation operator NEW is a constructor operator that creates an object (anonymous data object or

    instance of a class).

    ... NEW dtype( value ) ...

    creates an anonymous data object of data type dtype and passes a value to the created object. The value

    construction capabilities cover structures and internal tables (same as those of the VALUE operator).

    ... NEW class( p1 = a1 p2 = a2 ... ) ...

    creates an instance of class class and passes parameters to the instance constructor.

    ... NEW #( ... ) ...

    creates either an anonymous data object or an instance of a class depending on the operand type.

    You can write a compnent selector -> directly behind NEW type( ... ).

    http://scn.sap.com/people/horst.keller
  • 7/25/2019 Abap News for Release 740 Constructor Operator New

    2/9

    ABAP Development: ABAP News for Release 7.40 - Constructor Operator NEW

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

    2

    Example for data objects

    Before Release 7.40

    FIELD-SYMBOLS TYPE data.

    DATA dref TYPE REF TO data.

    CREATE DATA dref TYPE i.

    ASSIGN dref->* TO .

    = 555.

    With Release 7.40

    DATA dref TYPE REF TO data.

    dref = NEW i( 555 ).

    Example for instances of classes

    Before Release 7.40

    DATA oref TYPE REF TO class.

    CREATE OBJECT oref EXPORTING ...

    With Release 7.40

    Either

    DATA oref TYPE REF TO class.

    oref = NEW #( ... ).

    or with an inline declaration

  • 7/25/2019 Abap News for Release 740 Constructor Operator New

    3/9

    ABAP Development: ABAP News for Release 7.40 - Constructor Operator NEW

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

    3

    DATA(oref) = NEW class( ... ).

    This is the kind of statement NEW is made for. You can also pass it to methods expecting references.

    And one for the road ...

    TYPES: BEGIN OF t_struct1,

    col1 TYPE i,

    col2 TYPE i,

    END OF t_struct1,

    BEGIN OF t_struct2,

    col1 TYPE i,

    col2 TYPE t_struct1,

    col3 TYPE TABLE OF t_struct1 WITH EMPTY KEY,

    END OF t_struct2,

    t_itab TYPE TABLE OF t_struct2 WITH EMPTY KEY.

    DATA(dref) =

    NEW t_itab( ( col1 = 1

    col2-col1 = 1

    col2-col2 = 2

    col3 = VALUE #( ( col1 = 1 col2 = 2 )

    ( col1 = 3 col2 = 4 ) ) )

    ( col1 = 2

    col2-col1 = 2

    col2-col2 = 4

    col3 = VALUE #( ( col1 = 2 col2 = 4 )

    ( col1 = 6 col2 = 8 ) ) ) ).

    14702 Views Tags: abap

    Horst Kellerin response to Thomas Mateckion page 4

    13 Jan, 2016 10:22 AM

    Hi Thomas,

    http://scn.sap.com/people/horst.kellerhttp://scn.sap.com/people/horst.kellerhttp://scn.sap.com/community/abap/blog/tags#/?tags=abap
  • 7/25/2019 Abap News for Release 740 Constructor Operator New

    4/9

    ABAP Development: ABAP News for Release 7.40 - Constructor Operator NEW

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

    4

    as for all constructor expressionsthe data type must be known at compile time. Otherwise, the expression

    cannot be evaluated. E.g. how would it be possible to pass parameters in the brackets if the type would be

    generic or even dynamic.

    Best

    Horst

    Thomas Mateckiin response to Horst Kelleron page 4

    13 Jan, 2016 12:12 AM

    Hi Horst,

    Can NEW be used with a type handle(from RTT) or if not currently are there any plans to make this possible?

    Something like...

    Before ...

    CREATE DATA dref TYPE HANDLE lr_data_descr.

    With ...

    dref = NEW lr_data_descr( ).

    Or even ...

    dref = NEW cl_abap_tabledescr=>create(...)( ).

    vamsilakshman pendurtiin response to Horst Kelleron page 4

    30 Nov, 2015 9:00 AM

    Thanks Horst ,

    Vamsi

    Horst Kellerin response to vamsilakshman pendurtion page 4

    29 Nov, 2015 2:31 PMNEW can also replace CREATE DATA.

    Horst

    vamsilakshman pendurtiin response to Horst Kelleron page 5

    28 Nov, 2015 6:06 PM

    Thanks for the reply Horst ,

    http://scn.sap.com/people/vamsilakshman.pendurtihttp://scn.sap.com/people/vamsilakshman.pendurtihttp://scn.sap.com/people/horst.kellerhttp://scn.sap.com/people/horst.kellerhttp://scn.sap.com/people/vamsilakshman.pendurtihttp://scn.sap.com/people/vamsilakshman.pendurtihttp://scn.sap.com/people/thomas.matecki2http://scn.sap.com/people/thomas.matecki2http://help.sap.com/abapdocu_750/en/index.htm?file=abenconstructor_expressions.htm
  • 7/25/2019 Abap News for Release 740 Constructor Operator New

    5/9

    ABAP Development: ABAP News for Release 7.40 - Constructor Operator NEW

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

    5

    You mean to say , NEW Operator is Only for OOPs Concept ..

    Instead of Instantiate the Reference Object for a class we use NEW Operator ...In one statement...

    Vamsi

    Horst Kellerin response to vamsilakshman pendurtion page 5

    28 Nov, 2015 4:45 PM

    Hi,

    NEW creates an object like CREATE OBJECT and a value, VALUES creates only a value. NEW returns a

    reference to the object created. VALUE retrurns a value.

    Horst

    vamsilakshman pendurti28 Nov, 2015 12:33 PM

    Hi Horst ,

    Nice Blog... good to learn new things ....

    What is the exact difference between NEW and VALUE Constructor Operators ....

    i thought Both are same and behaves same ...

    Thanks & Regards ,

    Vamsilakshman

    Horst Kellerin response to Christian Lechneron page 529 Jul, 2015 8:53 AM

    I'm not authorized to make statements about upcoming releases, sorry ...

    Christian Lechnerin response to Horst Kelleron page 5

    28 Jul, 2015 4:53 PM

    Hi,

    is this feature planned for upcoming service packs or for the new NW 750?

    Cheers

    Christian

    Horst Kellerin response to Hannes Ladsttteron page 6

    6 May, 2015 5:02 PM

    Hello,

    No, this is not possible. For all constructor operators the type must be known at compile time, eihter given

    directly or inferred from the operand position.

    http://scn.sap.com/people/horst.kellerhttp://scn.sap.com/people/horst.kellerhttp://scn.sap.com/people/christian.lechnerhttp://scn.sap.com/people/christian.lechnerhttp://scn.sap.com/people/horst.kellerhttp://scn.sap.com/people/horst.kellerhttp://scn.sap.com/people/vamsilakshman.pendurtihttp://scn.sap.com/people/vamsilakshman.pendurtihttp://scn.sap.com/people/horst.kellerhttp://scn.sap.com/people/horst.keller
  • 7/25/2019 Abap News for Release 740 Constructor Operator New

    6/9

  • 7/25/2019 Abap News for Release 740 Constructor Operator New

    7/9

    ABAP Development: ABAP News for Release 7.40 - Constructor Operator NEW

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

    7

    Thanking You All.

    Horst Kellerin response to Ankit Sharmaon page 7

    29 Jul, 2013 8:58 AM

    Hi Ankit,

    Can we expect one special class for Integer and other data types (some sort of Wrapper

    Class )

    No, this is not planned. In ABAP (as a 4GL language) we will stay with the concept of data types and data

    objects as instances of data types on the one hand side and with classes and objects as instances of classes

    at the other hand side (where classes and data are in the same namespace).

    With the type concept, you can create complex types (structures and tables) and you have ABAP statements

    and expressions (that play the role of library methods from other languages) working with data objects of these

    types. Classes are "modularization" units for (business) implementations that use these statements for workingwith data objects.

    Ankit Sharma

    27 Jul, 2013 8:38 PM

    Hi Horst,

    Thanks a lot, for the info.

    Well, ABAP looks very much similar to Java now.

    Just wanted to know one thing,

    Since we will be using.

    dref = NEW i( 555 ).

    Can we expect one special class for Integer and other data types (some sort of Wrapper Class ), as we have in

    couple of other OOPS supporting languages ?

    Thanks a lot again.

    I am loving it ....!!!!!!!!!

    Marc Augustin

    29 May, 2013 8:44 AM

    Hi Horst,

    http://scn.sap.com/people/marc.augustin2http://scn.sap.com/people/marc.augustin2http://scn.sap.com/people/ankit.sharma2http://scn.sap.com/people/ankit.sharma2http://scn.sap.com/people/horst.kellerhttp://scn.sap.com/people/horst.keller
  • 7/25/2019 Abap News for Release 740 Constructor Operator New

    8/9

    ABAP Development: ABAP News for Release 7.40 - Constructor Operator NEW

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

    8

    thanks for sharing this. I really like the new features. Comming from other programming languages, I'm happy

    to see these things like the NEW operator finally being available in ABAP, too. Can't wait to have a 7.40

    somewhere to try out these new features, also the new expression posibilities.

    Cheers,

    Marc

    Horst Kellerin response to Jacques Nomssion page 8

    26 May, 2013 1:29 PM

    1. Why not? DATA(text) = 'aaa' gives a field of type c length 3.2. NEW|VALUE|CONV|CAST|REF|EXACT|COND|SWITCH3. Finally, someone asked. Yes, new in 7.40. Explicitly declaring an empty key for standard tables. Better

    than a generic key or the default key in many cases. I''ll come back to that later.

    Jacques Nomssi

    24 May, 2013 6:42 PM

    Hello Horst,

    I have bin awaiting this since I saw the slide in the NW 7.4 announcement. Please allow me to ask...

    1. type inference: DATA(text) = `...` creates a string, but DATA(text) = '...' will not work, won't it?

    2. are there more operators than NEW and VALUE ?3. what is this WITH EMPTY KEY, is it new in 7.4?

    thanks for taking the time.

    JNN

    Manu Bhatnagar24 May, 2013 1:49 PM

    Hi Horst Keller,

    nice piece of information. It will reduce the lines of code and also make using OOPs concepts easier.

    Thanks for sharing the info,

    Cheers,

    Manu B

    Custodio de Oliveira

    24 May, 2013 1:42 PM

    Hi again Horst,

    I think it's a very good idea you are releasing these new stuff in daily-sh blogs. Otherwise my brain would melt.

    Keep them coming!

    http://scn.sap.com/people/custodio.deoliveirahttp://scn.sap.com/people/horst.kellerhttp://scn.sap.com/people/custodio.deoliveirahttp://scn.sap.com/people/custodio.deoliveirahttp://scn.sap.com/people/horst.kellerhttp://scn.sap.com/people/manu.bhatnagarhttp://scn.sap.com/people/manu.bhatnagarhttp://scn.sap.com/people/jacques.nomssihttp://scn.sap.com/people/jacques.nomssihttp://scn.sap.com/people/horst.kellerhttp://scn.sap.com/people/horst.keller
  • 7/25/2019 Abap News for Release 740 Constructor Operator New

    9/9