30

Possible Duplicate:
Is there, or is there ever going to be, a conditional operator in Delphi?

I understand Delphi doesnt have the ternary operator as in C#. i.e. ?:

So how best to represent this function call? Whats the cleanest method out there?

Would be very nice if there is any code out there that can be used INSTEAD of writing a separate function? If not, whats the most efficient and/or cleanest code representation of it?

Community
  • 1
  • 1
Simon
  • 9,197
  • 13
  • 72
  • 115
  • 1
    There is no such thing as "*the* ternary operator". A ternary operator is *any* operator that accepts three operands. You probably are referring to a specific ternary operator, namely, `?:`. – Andreas Rejbrand Mar 09 '11 at 06:09
  • Thanks Mikael, voted to close. – Simon Mar 09 '11 at 06:26
  • 3
    @Andreas, if there is only one ternary operator, then it is *the* ternary operator. We can also call it by its name, the conditional operator. Similarly, there is only one person who posted the first comment on this question. We can either call him the person who posted the first comment on this question, or we can call him by his name, Andreas. – Rob Kennedy Mar 09 '11 at 16:09
  • To answer the "is there ever going to be part", my guess is no, there won't be. It's just not "Pascal-y". – Nick Hodges Mar 09 '11 at 17:06
  • @Rob: Yes, you are right, of course. – Andreas Rejbrand Mar 09 '11 at 18:16

3 Answers3

49

Of course you can use

IfThen(SomeBooleanExpression, IfTrueReturnValue, IfFalseReturnValue)

where the return values are numeric (uses Math) or string (uses StrUtils). But notice that this will evaluate both arguments in all cases -- there is no lazy evaluation, so it is not as efficient as the ?: operator in C#, where only the right operand is evaluated.

So you cannot do

y := IfThen(x <> 0, 1/x, 0)

The best thing is to stick with an ordinary

if x <> 0 then y := 1/x else y := 0;
Andreas Rejbrand
  • 105,602
  • 8
  • 282
  • 384
  • Damn was hoping to avoid all my small `if then elses` :( – Simon Mar 09 '11 at 06:24
  • 3
    Don't know whether this is still the case, but the drawback of using IfThen used to be (/is) that all parts are always evaluated and you cannot avoid access violations like you do with "normal" if statements. IE `if Assigned(A) then X := A.DoSome else X := '';` – Marjan Venema Mar 09 '11 at 08:05
  • 3
    Ah, see now that you mentioned lazy evaluation, but used division by zero as the example... – Marjan Venema Mar 09 '11 at 08:07
  • if the two values where expressions instead (anonymous functions) one could do short circuit evaluation – George Birbilis Aug 29 '21 at 15:00
5

The closest you can get to a ternary operator is:

if (condition) then <statement> else <statement>;

As far as I remember, there is no ternary operator in Delphi.

A1rPun
  • 16,287
  • 7
  • 57
  • 90
Unmanned Player
  • 1,109
  • 9
  • 30
2

Try the Iff from Jedi:

function Iff(const Condition: Boolean; const TruePart: string; const FalsePart: string): string; overload;
function Iff(const Condition: Boolean; const TruePart: Char; const FalsePart: Char): Char; overload;
function Iff(const Condition: Boolean; const TruePart: Byte; const FalsePart: Byte): Byte; overload;
function Iff(const Condition: Boolean; const TruePart: Integer; const FalsePart: Integer): Integer; overload;
function Iff(const Condition: Boolean; const TruePart: Cardinal; const FalsePart: Cardinal): Cardinal; overload;
function Iff(const Condition: Boolean; const TruePart: Float; const FalsePart: Float): Float; overload;
function Iff(const Condition: Boolean; const TruePart: Boolean; const FalsePart: Boolean): Boolean; overload;
function Iff(const Condition: Boolean; const TruePart: Pointer; const FalsePart: Pointer): Pointer; overload;
function Iff(const Condition: Boolean; const TruePart: Int64; const FalsePart: Int64): Int64; overload;
function Iff(const Condition: Boolean; const TruePart: Variant; const FalsePart: Variant): Variant; overload;
CloudyMarble
  • 36,908
  • 70
  • 97
  • 130
  • 3
    Yes, this is an "extended" version of the RTL's `IfThen` functions. But it still suffers from the same problem as the `IfThen` functions, as compared with `?:`. – Andreas Rejbrand Mar 09 '11 at 06:19
  • note that apart from https://docwiki.embarcadero.com/Libraries/Sydney/en/System.StrUtils.IfThen , there's also https://docwiki.embarcadero.com/Libraries/Sydney/en/System.Math.IfThen – George Birbilis Oct 21 '21 at 09:45