‎2021 Oct 11 12:13 PM
Hi ABAP community,
I'm using the SWITCH statement in combination with && to create a string.
Somehow when using more than one condition, it looses a letter. Following my simple example report:
REPORT z_test_switch.
DATA: test_1 TYPE string,
test_2 TYPE string,
test_3 TYPE string.
DATA(lv_content_type) = 'PNG'.
* Exprected result: IMAGE_TO_PDF
* success: IMAGE_TO_PDF
test_1 = SWITCH #( lv_content_type WHEN 'PNG' THEN 'IMAGE' ).
test_1 = test_1 && '_TO_PDF'.
WRITE / test_1.
* success: IMAGE_TO_PDF
test_2 = SWITCH #( lv_content_type WHEN 'PNG' THEN 'IMAGE' ) && '_TO_PDF'.
WRITE / test_2.
* fails: IMAG_TO_PDF
test_3 = SWITCH #( lv_content_type WHEN 'DOC' THEN 'WORD'
WHEN 'PNG' THEN 'IMAGE' ) && '_TO_PDF'.
WRITE / test_3.
It's loosing the last letter from IMAGE and only returns IMAG_TO_PDF instead of IMAGE_TO_PDF when using a switch with more than one condition.
I'm on System:

I can't figure out, what it wrong in the last testcase. Is this may be a bug?
Glad about any hints!
Regards
Nico
‎2021 Oct 11 12:46 PM
Hi Nico,
This is the result of the way you've written the SWITCH statement.
The data type of the resulting variable is determined at runtime ('SWITCH #').
Since the first condition results in 'WORD', runtime puts the resulting substring of the SWITCH statement in a variable of length '4', which it then concatenates with '_TO_PDF'.
Notice the difference in the following:
"fails: IMAG_TO_PDF: output is mapped to CHAR4 ('WORD')
test_3 = SWITCH #( lv_content_type WHEN 'DOC' THEN 'WORD'
WHEN 'PNG' THEN 'IMAGE' ) && '_TO_PDF'.
"success: IMAGE_TO_PDF: output of SWITCH statement is string
test_3 = SWITCH string( <br> lv_content_type WHEN 'DOC' THEN 'WORD'
WHEN 'PNG' THEN 'IMAGE' ) && '_TO_PDF'.
"success: IMAGE_TO_PDF: output of SWITCH statement is CHAR12
"because of 'WORDDOCUMENT'
test_3 = SWITCH #(
lv_content_type WHEN 'DOC' THEN 'WORDDOCUMENT'
WHEN 'PNG' THEN 'IMAGE' ) && '_TO_PDF'.
"fails: IMA_TO_PDF: output is mapped to CHAR3 ('DOC')
test_3 = SWITCH #( lv_content_type WHEN 'DOC' THEN 'DOC'
WHEN 'PNG' THEN 'IMAGE' ) && '_TO_PDF'.
‎2021 Oct 11 12:46 PM
Hello,
you used the generic type # in your expression. It looks like the 4 character long conditional value 'WORD' causes a return value of type character with length 4. BTW, if you swap the expression to ...WHEN 'PNG' THEN 'IMAGE WHEN 'DOC' THEN 'WORD' it works (I guess because the result will be typed as CHAR5, according to the value given after the first THEN). It also works when you use the concrete type string instead of #.
This is written in the documentation which explains what happens here:
If the data type of the operand after the first THEN is known statically and matches the generic type of the formal parameter, this data type is used.
So, as it is documented it is not a bug 😉
From my point of view it is better to prefer a specific type over a generic one, if possible.
test_3 = SWITCH string( lv_content_type WHEN 'DOC' THEN 'WORD'
WHEN 'PNG' THEN 'IMAGE' ) && '_TO_PDF'.
‎2021 Oct 11 12:46 PM
Hi Nico,
This is the result of the way you've written the SWITCH statement.
The data type of the resulting variable is determined at runtime ('SWITCH #').
Since the first condition results in 'WORD', runtime puts the resulting substring of the SWITCH statement in a variable of length '4', which it then concatenates with '_TO_PDF'.
Notice the difference in the following:
"fails: IMAG_TO_PDF: output is mapped to CHAR4 ('WORD')
test_3 = SWITCH #( lv_content_type WHEN 'DOC' THEN 'WORD'
WHEN 'PNG' THEN 'IMAGE' ) && '_TO_PDF'.
"success: IMAGE_TO_PDF: output of SWITCH statement is string
test_3 = SWITCH string( <br> lv_content_type WHEN 'DOC' THEN 'WORD'
WHEN 'PNG' THEN 'IMAGE' ) && '_TO_PDF'.
"success: IMAGE_TO_PDF: output of SWITCH statement is CHAR12
"because of 'WORDDOCUMENT'
test_3 = SWITCH #(
lv_content_type WHEN 'DOC' THEN 'WORDDOCUMENT'
WHEN 'PNG' THEN 'IMAGE' ) && '_TO_PDF'.
"fails: IMA_TO_PDF: output is mapped to CHAR3 ('DOC')
test_3 = SWITCH #( lv_content_type WHEN 'DOC' THEN 'DOC'
WHEN 'PNG' THEN 'IMAGE' ) && '_TO_PDF'.
‎2021 Oct 11 1:19 PM
Hi Laurens, hi Jan,
thank you both! That makes sense. I was expecting, that the generic type gets specified, after the swtich is returning a value. Because I was also testing the following:
test_5 = SWITCH #( lv_content_type WHEN 'DOC' THEN 5
WHEN 'PNG' THEN 'IMAGE' ).
WRITE / test_5.
And this is creating test_5 as string, not as number.
As always, reading the documentation helps.
thanks & best Regards
Nico