Many have asked, over the years, why Delphi does not include a ternary operator. Now Delphi 13 Florence adds the conditional operator to the language, which is that ternary operator.
The syntax is simple:if <cond> then <thenValue> else <elseValue>
In use, you may see:X := if Left < 100 then 22 else 45;
or
ShowMessage(if Failed then 'Failed' else 'Success');
The conditional operator is an expression, so it is evaluated, and the value is returned.
This is a welcome addition to the language, and when used well, it will increase clarity in your code, generally where you will be condensing inline processing. In cases where the condition itself is complex, or where the values to be returned are not simple and small, then the use of explaining variables will be useful. Or you may simply choose to employ the more conventional coding, without the conditional operator.
Some years ago, the IfThen() functions were added to the Delphi libraries. These gave some convenience, but were just functions with specific areas of support. The new conditional operator is altogether different, as may be seen in the help page which presents a substantial chart of the various types and how they are handled by the conditional operator. There is much content on that page, and I recommend you give it your attention.
Although the IfThen() functions were convenient, the conditional operator is a first-class member of the language, as this table of types clearly shows, and adds real value to the language.
| Type 1 | Type 2 | Result type |
|---|---|---|
| AnsiChar | AnsiChar | AnsiChar |
| AnsiString | AnsiString, AnsiChar | AnsiString |
| Array | Array | Array of the same element type. |
| Boolean types | Boolean types | Common Boolean type or Boolean type. |
| Class reference | Class reference, nil | Common type, Base type, or TClass. |
| Dynamic array | Dynamic array | Common dynamic array type. |
| Enum type | Enum type | Common enum type. |
| File | File | Error (use pointer to File type). |
| Instance | Instance, nil | Common type, Base type, or TObject. |
| Integral types | Integral types | Common integral type, or the type that covers both ranges. |
| Integral types | Real types | Real type. |
| Interface | Interface, nil | Common type, Base type, or IInterface. |
| nil | AnsiChar, ShortString, AnsiString, WideChar, WideString | Pointer to WideChar |
| Object | Object | Object type. |
| Pointer to AnsiChar | AnsiChar, ShortString, AnsiString literal | Pointer to AnsiChar |
| Pointer to AnsiChar | Pointer to WideChar literal | Pointer to WideChar |
| Pointer to WideChar | AnsiChar, ShortString, AnsiString, WideChar, WideString, UnicodeString | UnicodeString |
| Pointer types | Pointer types | Common pointer type, or Pointer type. |
| Procedure | Procedure | Procedure type, if they have the same signature. |
| Procedure of object | Procedure of object | Procedure for object type if they have the same signature. |
| Real types | Real types | The real type with the greater precision. |
| Record | Record | Record type. |
| Reference to procedure Anonymous Method | Reference to procedure Anonymous Method | Reference to the procedure type, if they have the same signature. |
| Set | Set | Common set type, or large set type if list literals. |
| Short string | Short string, AnsiChar | Longer short string |
| Text | Text | Error (use pointer to Text type). |
| UnicodeString | UnicodeString, WideString, AnsiString, Short string, WideChar, AnsiChar | UnicodeString |
| Variant | Variant | Variant type. |
| WideChar | WideChar | WideChar |
| WideChar | AnsiChar, AnsiString | UnicodeString |
| WideString | WideString, AnsiString, WideChar, AnsiChar | WideString |