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

ABAP: Problems with constructor expression in case statement

jrgkraus
Active Contributor
1,663

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?

1 ACCEPTED SOLUTION
Read only

retired_member
Product and Topic Expert
Product and Topic Expert
1,438

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

4 REPLIES 4
Read only

retired_member
Product and Topic Expert
Product and Topic Expert
1,439

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

Read only

1,438

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.


Read only

1,438

Oh wow -> the bug is even more severe

Read only

retired_member
Product and Topic Expert
Product and Topic Expert
1,438

Corrected in an upcoming release (but no patch).

Thanks for notifying!