5

Here is one for you.

How come that this one does not compile

REPORT ZZY.

TYPES: my_int TYPE x LENGTH 4,
  my_int_table TYPE STANDARD TABLE OF my_int WITH EMPTY KEY.

DATA(g_tab_my_int) = VALUE my_int_table( ( 2 ) ).

and this one does?

REPORT ZZY.

TYPES: my_int TYPE x LENGTH 4,
  my_int_table TYPE STANDARD TABLE OF my_int WITH EMPTY KEY.

DATA(g_tab_my_int) = VALUE my_int_table( ( 2 * 1 ) ).
Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Jagger
  • 10,350
  • 9
  • 51
  • 93

1 Answers1

7

The ABAP documentation for inside VALUE dtype|#( line | {LINES OF itab ...} ) says:

If a data object is specified for line, this object must be compatible with the row type.

If an expression (built-in function, functional method, calculation expression, constructor expression, or table expression) is specified for line, the result of the expression must be convertible to the row type.

In your example:

  • "2" being a numeric literal so being a data object, it's valid only if it's compatible with the type i.e. it's exactly of the same type.
  • "1 * 2" being an expression, it's valid because a conversion rule exists from the type I to the type X.

You may enter a shorter expression like "+ 2" : VALUE my_int_table( ( + 2 ) ).

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
  • 1
    Caveat: Don't use this workaround in tight loops or otherwise performance-critical code. ABAP does not do constant folding, so the arithmetic expression will be evaluated at runtime every time this code runs. Using CONV to force conversion of the literal would be more advisable. – ACuriousMind Apr 05 '19 at 21:07