1

We all know this fancy new pseudo-ternary operator as COND:

COND #( WHEN 1 = 1 THEN something ELSE everything ).

However, recently during the practices I noticed weird thing with typing of returned variable: it always get type of the first THEN operand and this had been confirmed by the ABAP help.

If the operand type is not fully identifiable, an operand with a statically identifiable type must be specified after the first THEN (except when passing the constructor parameter to an actual parameter with generically typed formal parameter). This type is then used.

DATA(val) = COND #( WHEN quantity NE '0.00' THEN CONV wrbtr( quantity ) ELSE '' )

In this example val variable will always have packed type disregard of the value of quantity.

How can we achieve conditional typing here? I.e. WRBTR type with non-empty quantity and string type with empty quantity. It is very often a requirement during passing internal data to external systems, external methods/FMS, as well as external formats (Excel, CSV).

Is there some syntax I am missing with COND and CONV operators? Can we achieve this with their help? Or maybe there is some more fancier syntax except

IF quantity NE '0.00'.
  val = VALUE wrbtr(  ).
ELSE.
  val = VALUE string(  ).
ENDIF.
Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170
Suncatcher
  • 10,355
  • 10
  • 52
  • 90
  • 1
    "Conditional typing"? But a variable must have a complete type anyway, defined at compile time. It cannot be decided at run time. And that's not dependent of COND or any other constructor operator. The only true "conditional typing" is RTTC (CREATE DATA). – Sandra Rossi Jun 26 '18 at 17:44
  • Indeed. But inline declaration like in my example perfectly allows full typing, and the only glitch is conditionality, which I cannot achieve. – Suncatcher Jun 27 '18 at 04:52
  • I don't understand if you want "conditional conversion" or "conditional typing". The latter one is impossible as per ABAP "philosophy". If you want conditional conversion, it's of course possible, but you can't do a shorter code than the one you propose. If you could provide a "full" example/code, that would be easier to discuss. – Sandra Rossi Jun 27 '18 at 05:30
  • `but you can't do a shorter code than the one you propose.` you mean the last one with IF? Can we achive smth more beautiful with COND or CONV? – Suncatcher Jun 27 '18 at 05:51
  • `If you could provide a "full" example/code, that would be easier to discuss.` my example as I stated in question is to pass variable to the method which accepts parameter of any type, so I want construct my type on the fly. – Suncatcher Jun 27 '18 at 05:54
  • `val` can be only one type at compile time (or don't use a variable, use CREATE DATA + a field symbol), so the best you can do is to have val being a string : `DATA(val) = COND #( WHEN quantity NE '0.00' THEN CONV string( quantity ) ELSE ' ' ).` – Sandra Rossi Jun 27 '18 at 06:47
  • @Jagger I'd rather not to name it a full duplicate :) I revealed that this is not a "strange behavior" but rather normal behavior as per the idea of ABAP maintainers (=ABAP help). I am seeking the replacement of COND construction. – Suncatcher Jun 27 '18 at 11:13
  • @Suncatcher There is also [this](https://stackoverflow.com/questions/37568115/conversion-exception-while-working-with-constructor-expressions) question asked and answered over 2 years ago, soooo still a duplicate. :) – Jagger Jun 27 '18 at 11:58
  • Yes, totally missed this 2yr question :) Though, both those question didn't propose any alternative to `COND` and that's exactly what my question is about. – Suncatcher Jun 28 '18 at 16:23
  • Can we achieve something like conditional VALUE #() op? I.e. operator that creates data depending on some condition? – Suncatcher Jun 28 '18 at 16:24

1 Answers1

3

There is no conditional typing in ABAP. As a fully typed language, every variable needs a definite type at compile time.

The example you provided doesn't work, by the way:

DATA quantity TYPE wrbtr.
DATA val TYPE wrbtr.
IF quantity NE '0.00'.
  val = '3.12'.
ELSE.
  val = VALUE string(  ).
ENDIF.

val will still have type wrbtr, even if the ELSE is executed. ABAP first converts the value to string, then on to the target wrbtr.

Florian
  • 4,821
  • 2
  • 19
  • 44