Constructor Expressions in ABAP 7.4 [Part I]

0
1420

Dear saplearners, in this blog post we are going to learn about ABAP 7.4 new syntax and more about constructor expressions and constructor operators

Constructor Expressions

A new type of expression called constructor expressions are introduced to construct results of specified types and their contents.

Lets quickly glimpse through SAP’s definition for constructor expression


A constructor expression comprises of

  • a predefined constructor operator,
  • a data type or object type that matches the operator and that can be derived implicitly from the operand position using #,
  • and type-specified parameters specified in parentheses.

Before we delve in how to build the constructor expressions, first we need to know about the constructor operators.

Constructor Operators

Below are the list of constructor operators which we can use to build constructor expression in ABAP 7.4

  • NEW
  • VALUE
  • REF
  • CORRESPONDING
  • CONV
  • EXACT
  • CAST
  • REDUCE
  • FILTER
  • COND
  • SWITCH

Each of these constructor operator have its own purpose which we will learn in-detail with some example ABAP code snippets.

As the list of operators are more to cover in one blog post, we are going to learn to use these constructor operators to build constructor expression in 2 blog posts.

In this first part of Constructor Expressions in ABAP7.4, lets see these constructor operators

1. NEW

NEW is an instance operator that you can use to create instance of a class or data-object. Lets see some constructor expressions that we can build using this operator.

Example#1 – Class

In the above example, we used instance operator NEW to create an instance for a class. ABAP code lines 10 & 11 are old way syntax and we all know about it, but look at the ABAP code at line-15 is the new syntax in ABAP 7.4

We can also use NEW instance operator for instantiating or initial values for Structures, Internal Tables, Types and Data Types but i prefer using the VALUE operator and lets see we can do this

2. VALUE

A constructor expression with the value operator VALUE creates a result of a data type specified using type.The operator can be used to create initial values for all Types, Structures and Internal Tables. Lets see the usage of the operator with some code snippets

Example#1 – Initializing the Structures

Before 7.4 we have to use the below syntax to initialize the structure values. As the complexity of structure with grows with more fields and nested structures it will be very difficult

With ABAP 7.4 new syntax the initializing can be made easy with new syntax like below

In the above abap code snippet there are two options to initialize the structure first one DATA(…) = VALUE type(…) is explicit and later VALUE #(…) is implicit one. Both can serves the same purpose and can use any one of them

Example#2 – Initializing the Internal Tables

Before 7.4, we have to use APPEND statement to initialize the internal tables

With ABAP 7.4 new syntax the initializing can be made easy with new syntax like below

we can achieve the same results using inline structure initialization like below and a short-form as well.

 

 

 

Congrats..! you learned about ABAP 7.4 new syntax, try this out in your next ABAP program and let me know how you feel about it.

Please feel free to comment and let us know your feedback. Subscribe for updates

If you liked it, please share it! Thanks!