Skip to Content
4

ABAP: Problems with constructor expression in case statement

Dec 05, 2017 at 08:35 AM

85

avatar image

The following coding produces a syntax error "WHEN must be the first statement after a CASE statement.":

   case reduce i( init count = 0
      for s_mat in mt_materials where ( lead = abap_true )
      next count = count + 1 ).
      when 0.
        raise exception type lcx_no_lead_mat.
      when 1.
        " OK.
      when others.
        raise exception type lcx_multiple_lead.
    endcase.

Whilst this version passes without problems:

    data(lv_count) = reduce i( init count = 0
      for s_mat in mt_materials where ( lead = abap_true )
        next count = count + 1 ).
    case lv_count.
      for s_mat in mt_materials where ( lead = abap_true )
      next count = count + 1 ).
      when 0.
        raise exception type lcx_no_lead_mat.
      when 1.
        " OK.
      when others.
        raise exception type lcx_multiple_lead.
    endcase.

ABAP docu says that the operand of a case statement is a general expression position, so I should be able to use reduce in this place. Where am i doing wrong?

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

2 Answers

Horst Keller
Dec 05, 2017 at 09:06 AM
4

What release are you working on?

The following gives a syntax warning for the last CASE down to 7.40, SP08, but not an error.

CASE VALUE i( ).
  WHEN 1.
ENDCASE.

CASE REDUCE i( INIT x = 0
               FOR i = 1 UNTIL i > 1
               NEXT x = 1 ).
  WHEN 1.
ENDCASE.

DATA itab TYPE TABLE OF i.
CASE REDUCE i( INIT x = 0
               FOR wa IN itab
               NEXT x = 1 ).
  WHEN 1.
ENDCASE.

The warning is strange, ->development

Show 2 Share
10 |10000 characters needed characters left characters exceeded

I am working on 7.40 SP16. It's true that there is only a warning in your code, but as soon as you put the code inside a method, the warning becomes an error:

class lcl_a definition.
  public section.
    methods main.
endclass.

class lcl_a implementation.
  method main.
    case value i( ).
      when 1.
    endcase.
    case reduce i( init x = 0
                   for i = 1 until i > 1
                   next x = 1 ).
      when 1.
    endcase.
    data itab type table of i.
    case reduce i( init x = 0
                   for wa in itab
                   next x = 1 ).
      when 1.
    endcase.
  endmethod.
endclass.


3

Oh wow -> the bug is even more severe

3
Horst Keller
Dec 05, 2017 at 05:37 PM
2

Corrected in an upcoming release (but no patch).

Thanks for notifying!

Share
10 |10000 characters needed characters left characters exceeded