‎2019 Nov 22 9:04 AM
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.
‎2019 Nov 22 9:59 AM
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.
‎2019 Nov 22 9:59 AM
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.
‎2019 Nov 22 1:17 PM
Enumerations are well explained in the blog post ABAP News Release 7.51 – Enumerations, by Horst Keller, on 2016/10/10.
‎2019 Nov 22 2:02 PM
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 😄
‎2019 Nov 22 2:33 PM
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.
‎2019 Nov 22 1:16 PM
val1 is of type ty_enum, so you should declare DATA z TYPE ty_enum.