Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Program Flow Logic →  Iteration Expressions →  REDUCE - Reduction Operator 

Reduce Operator, Type Inference

This example demonstrates a type inference for the conversion operator REDUCE.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    DATA txt TYPE c LENGTH 20.
    DATA num TYPE i.

    demo=>meth1( p = REDUCE #( INIT r1 = txt
                               FOR i = 1
                               UNTIL i > 9
                               NEXT r1 = r1 && 'x' ) ).
    demo=>meth1( p = REDUCE #( INIT r2 = num
                               FOR i = 1
                               UNTIL i > 9
                               NEXT r2 = r2 + 1 ) ).
    cl_demo_output=>line( ).

    demo=>meth2( p = REDUCE #( INIT r1 = txt
                               FOR i = 1
                               UNTIL i > 9
                               NEXT r1 = r1 && 'x' ) ).
    "demo=>meth2( p = REDUCE #( INIT r2 = num
    "                           FOR i = 1
    "                           UNTIL i > 9
    "                          NEXT r2 = r2 + 1 ) ). "not possible
    cl_demo_output=>line( ).

    demo=>meth3( p = REDUCE #( INIT r1 = txt
                               FOR i = 1
                               UNTIL i > 9
                               NEXT r1 = r1 && 'x' ) ).
    demo=>meth3( p = REDUCE #( INIT r2 = num
                               FOR i = 1
                               UNTIL i > 9
                               NEXT r2 = r2 + 1 ) ) ##type.
    cl_demo_output=>display( ).

Description

Passes constructor expressions with the conversion operator REDUCE to differently typed formal parameters of methods. In the case of generic formal parameters, special rules apply when identifying the operand type.

  • Fully typed formal parameter
When the method meth1 is called with a fully typed formal parameter, the operand type for # is identified using this parameter and the result of the reduction is converted to c with length 10 in both calls.
  • Formal parameter typed generically with c
The operand type for # is identified from the first declaration after INIT.
  • In the first call, the type c with length 20 of the declaration after INIT matches the generic type and is used.
  • It is not possible to perform a call with the type i of the declaration after INIT, since it does not match the typing and there are no inference rules for the generic type c.
  • Formal parameter typed generically with csequence
  • In the first call, the type c with length 20 of the declaration after INIT matches the generic type and is used.
  • In the second call, the type i of the declaration after INIT does not match the generic type and the type string is used. This is indicated by a syntax check warning.