Application Development and Automation 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: 
Read only

How does BASE TYPE in ENUM works?

former_member210008
Active Participant
0 Likes
2,097
TYPES ty_x TYPE x LENGTH 4.
TYPES:BEGIN OF ENUM ty_enum BASE TYPE ty_x,
        val0 VALUE IS INITIAL,
        val1 VALUE 1,
        val2 VALUE 2,
        val3 VALUE 32,
      END OF ENUM ty_enum.

DATA z TYPE ty_x.
z = val1.

The type of "VAL1" cannot be converted to the type of "Z"

So how base type works?.. I want to make some bit operations with my enum without CONV.

1 ACCEPTED SOLUTION
Read only

Former Member
1,987

Base type determines the internal representation of the enum type, but still, you need conversion between the enum and the underlying type.

TYPES:
  BEGIN OF ENUM size,
    s, m, l, xl, xxl,
  END OF ENUM size.

DATA: size TYPE size,
      int  TYPE i.

size = CONV #( 2 ).
int  = CONV #( xl ).

As per my understanding an enum type is optimal for value comparison. When used for that purpose the base type is quite irrelevant:

CLASS demo DEFINITION. 
  PUBLIC SECTION. 
    TYPES: 
      BEGIN OF ENUM size, 
        s, m, l, xl, xxl, 
      END OF ENUM size. 
    CLASS-METHODS main 
      IMPORTING size TYPE size. 
ENDCLASS. 

CLASS demo IMPLEMENTATION. 
  METHOD main. 
    CASE size. 
      WHEN s. 
        ... 
      WHEN m. 
        ... 
      WHEN l. 
        ... 
      WHEN OTHERS. 
        ... 
    ENDCASE. 
  ENDMETHOD. 
ENDCLASS. 

A "regular" constant should suit you more if you are mainly interested in working with operations of the underlying type:

TYPES ty_x TYPE x LENGTH 4.

CONSTANTS: BEGIN OF val,
        0 VALUE IS INITIAL,
        1 VALUE 1,
        2 VALUE 2,
        3 VALUE 32,
      END OF val.

DATA: z, y TYPE ty_x.

z = val-0.
y = val-0 + val-1.
5 REPLIES 5
Read only

Former Member
1,988

Base type determines the internal representation of the enum type, but still, you need conversion between the enum and the underlying type.

TYPES:
  BEGIN OF ENUM size,
    s, m, l, xl, xxl,
  END OF ENUM size.

DATA: size TYPE size,
      int  TYPE i.

size = CONV #( 2 ).
int  = CONV #( xl ).

As per my understanding an enum type is optimal for value comparison. When used for that purpose the base type is quite irrelevant:

CLASS demo DEFINITION. 
  PUBLIC SECTION. 
    TYPES: 
      BEGIN OF ENUM size, 
        s, m, l, xl, xxl, 
      END OF ENUM size. 
    CLASS-METHODS main 
      IMPORTING size TYPE size. 
ENDCLASS. 

CLASS demo IMPLEMENTATION. 
  METHOD main. 
    CASE size. 
      WHEN s. 
        ... 
      WHEN m. 
        ... 
      WHEN l. 
        ... 
      WHEN OTHERS. 
        ... 
    ENDCASE. 
  ENDMETHOD. 
ENDCLASS. 

A "regular" constant should suit you more if you are mainly interested in working with operations of the underlying type:

TYPES ty_x TYPE x LENGTH 4.

CONSTANTS: BEGIN OF val,
        0 VALUE IS INITIAL,
        1 VALUE 1,
        2 VALUE 2,
        3 VALUE 32,
      END OF val.

DATA: z, y TYPE ty_x.

z = val-0.
y = val-0 + val-1.
Read only

1,987

Enumerations are well explained in the blog post ABAP News Release 7.51 – Enumerations, by Horst Keller, on 2016/10/10.

Read only

1,987

sandra.rossi

Yes, it is, although I didn't see any practical example neither in the blog nor in the documentation why base type specification can be useful.

In my opinion if you are interested the values "behind" constants are the way to go. As a matter of fact I'd be more happy with an implicit conversion between integer and enum... But maybe I'm just attracted to C too much 😄

Read only

1,987

I agree that ABAP enumerations will probably be remembered as a nice try, but won't be adopted by developers because it's too much prone to errors and too complex to handle. I'm really disappointed and I'm now back at the structured constants.

Read only

Sandra_Rossi
Active Contributor
0 Likes
1,987

val1 is of type ty_enum, so you should declare DATA z TYPE ty_enum.