Skip to Content

Can we define constants based on existing structure?

Aug 08, 2017 at 08:56 AM


avatar image

Seems like a basic ABAP question, but...

I'd like to define a constant based on an existing structure. I was hoping the new ABAP syntax might do it and tried:

types: begin of fullname, 
         firstname type string,
         lastname  type string, 
       end of fullname. 

constants default_name type fullname value #( firstname = `John` 
                                              lastname  = `Doe` ).

But it seems we can't use constructor expressions in declarations.

Background: I'm trying to pre-populate some fields of a structure. Of course I can declare a matching structure explicitly:

constants: begin of default_name, 
             firstname type string value `John`,
             lastname  type string value `Doe`, 
           end of default_name.

But then it must be adapted if the original structure changes.

An alternative is to define components individually:

constants default_firstname type string value `John`.
constants default_lastname  type string value `Doe`.

But that's just clumsy.

So now I'm using a static attribute, but am curious if there's a syntax trick I've missed?

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

1 Answer

Best Answer
Horst Keller
Aug 08, 2017 at 10:03 AM

The start value val can either be specified as a literal or as a predefined constant.

No, constructor expressions in declarations are not possible.

Show 1 Share
10 |10000 characters needed characters left characters exceeded

Thanks, it seems like an odd omission of ABAP that we can't define ddic-structured constants, but it's only a minor inconvenience.