2

How can I execute in Delphi a conditional statements in a String?

In PHP there is something like this:

<?php 
echo "Hello (isset($name) ? $name : 'Guest')); 
?>
REALSOFO
  • 852
  • 9
  • 37
  • 1
    PHP has something *like* that, but not actually that. You're missing a closing quotation mark, but even with that added, it won't actually evaluate the `isset` expression in the string. When asking a question, please make sure the claims you make are true in the language you know before asking whether they're true in the language you don't. – Rob Kennedy Dec 06 '13 at 13:42
  • mmm I think I misread the question. Are you asking for an [eval](http://www.php.net/eval) counterpart in delphi? – whosrdaddy Dec 06 '13 at 13:45
  • 1
    @whosrdaddy What else could the question be? – David Heffernan Dec 06 '13 at 13:49
  • There's no string escaping/expansion/evaluation in Delphi, so definitely no ternary evaluation. – Marcus Adams Dec 06 '13 at 14:07

3 Answers3

4

I'm assuming you actually want to evaluate code that is not known until runtime. That's the only reason why you would have code in a string. If my assumption is correct, then you cannot do that readily in Delphi. Delphi is compiled. So in order to execute Delphi code you need to compile it.

You could consider using a scripting language for this part of your program. There are many available.

Of course, if all you want is a conditional operator in Delphi then there is none built in but the RTL provides IfThen:

function IfThen(AValue: Boolean; const ATrue: string; 
  AFalse: string = ''): string;

Description

Conditionally returns one of two specified values.

IfThen checks the expression passed as AValue and returns ATrue if it evaluates to true, or AFalse if it evaluates to false. In Delphi, if the AFalse parameter is omitted, IfThen returns 0 or an empty string when AValue evaluates to False.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • 2
    Just remember that both arguments are evaluated before executing the function, so it's not the same as the ternary operator (`?:`) in C or C#. – afrazier Dec 06 '13 at 14:36
  • 2
    @afrazier Yes that's true. By the way, the operator's name is actually the conditional operator. It is invariably named incorrectly as the ternary operator. – David Heffernan Dec 06 '13 at 14:42
  • @DavidHeffernan Doesn't Delphi actually have some ability to compile expressions at runtime? http://theroadtodelphi.wordpress.com/2012/07/06/a-quick-guide-to-evaluate-and-compile-expressions-using-the-livebindings-expression-evaluator/ https://bitbucket.org/NickHodges/nickdemocode/src/842eb3fc74d7/SampleExpressionEvaluator.dpr?at=default – alcalde Dec 06 '13 at 22:13
  • @alcalde evaluate at runtime rather than compile at runtime – David Heffernan Dec 07 '13 at 18:25
2

the closest thing you can get in Delphi is this :

Writeln('Hello ' + IIf(Name='', 'Guest', Name));

where IIf is defined as:

function iif(Test: boolean; TrueRes, FalseRes: string): string;
begin
 if Test then
  Result := TrueRes
 else
  Result := FalseRes;
end;

Please mind that this example only works with strings...

EDIT

As David suggested you can also use the IfThen function from the StrUtils unit

TLama
  • 75,147
  • 17
  • 214
  • 392
whosrdaddy
  • 11,720
  • 4
  • 50
  • 99
1

For a type independent IIF, use this:

function IIF(pResult: Boolean; pIfTrue: Variant; pIfFalse: Variant): Variant;
begin
  if pResult then
    Result := pIfTrue
  else
    Result := pIfFalse;
end;