NEW Operator – Structures and Internal Tables

Share on Facebook0Share on LinkedIn0Tweet about this on TwitterShare on Google+1
Please Share!!

In the previous post, we saw some features of NEW Operator. In this post, we would see the different ways to define structures and internal tables using NEW Operator.

[adToAppearHere]

1. For Structures
i) Anonymous data object

Can we declare NEW # as shown above for d_ref_struct?
Since, d_ref_struct is anonymous data object, it would not recognize matnr and werks. The error is: No type can be derived from the context for the operator “NEW”.

The right way is to define d_ref_struct as structured object as shown below.

ii) Structured data object

When “#” succeeds NEW, it means the object ref in the Left Hand Side determines the type.

2. For Internal Tables
i) Anonymous data object

Going by the example of structures shown above before internal table, this example 1 of internal table looks correct. But SAP does not like it.. 😛

What went wrong?
NEW ty_marc( ) is applicable for structure. So, cannot convert to internal table.

Lets try again by declaring a table type.

Here we made two changes.
i) Table Type tt_marc is defined.
ii) NEW tt_marc is used instead of NEW ty_marc

But, still the system gives error “A value of the generic type “TT_MARC” cannot be constructed“.

Let’s give one more shot.
Here we will add just 3 key words “WITH DEFAULT KEY” in Table Type declaration.

Bingooo!!!! WITH DEFAULT KEY did the trick. Anonymous data object is now an internal table.

Below is another example to define and populate internal table with anonymous object.

The content of the row is assigned to the data object. The object is created at the statement APPEND and the NEW data reference is appended directly to an internal table with the appropriate row type. The result is a table that references all new anonymous data objects of 10 rows.

ii) Structured data object

Hope with the above examples and snippets, you would be in the position to play around with NEW Operator. In our next post we would explore the VALUE Operator.

If you want to get notification about the newest posts, please subscribe. Your email is safe with us.
If you liked it, please share it! Thanks!

 

Image source: via Free Stock Photos foter.com

Share on Facebook0Share on LinkedIn0Tweet about this on TwitterShare on Google+1
Please Share!!

2 Comments on "NEW Operator – Structures and Internal Tables"

  1. Steve Oldner | June 25, 2015 at 12:05 pm |

    Thanks! Not sure why we would need this instead of the regular itabs.

    • Thank you Steve for your time and leaving a comment.

      The most important motivation behind this feature seems to be to ‘Catch up with trends’. We can have a separate discussion about the pros and cons. 🙂

      SAP says: These expressions would help as below:
      Declarative – What instead of How (http://en.wikipedia.org/wiki/Declarative_programming)
      Functional – less side effects (http://en.wikipedia.org/wiki/Functional_programming)
      Modern – catch up with trends

      For example, it reduces the number of lines of codes. In the below case, we do NOT need a complete statement CREATE DATA d_ref.

      Traditional
      FIELD-SYMBOLS < (fs)> TYPE data.
      DATA d_ref TYPE REF TO data.
      CREATE DATA d_ref TYPE i.
      ASSIGN d_ref->* TO < (fs)>.
      < (fs)> = 123. ” < (fs)> is not new.. 🙂 system not allowing me <>

      NEW
      DATA d_ref TYPE REF TO data.
      d_ref = NEW i( 123 ).

      Regards,
      Raju.

Comments are closed.