Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Can we define constants based on existing structure?

pokrakam
Active Contributor

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?

1 ACCEPTED SOLUTION

horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos

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

No, constructor expressions in declarations are not possible.

2 REPLIES 2

horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos

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

No, constructor expressions in declarations are not possible.

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