[fpc-devel] Proposal: IIF - IfThen Cond True False

Marc Weustink marc.weustink at cuperus.nl
Wed Jan 12 15:51:28 CET 2011


kingbizugo at gmail.com wrote:
> I come here humbly to ask for a new implementation, the IIF ?:, the IIF
> is a shortcut that make the coding a little faster, it works by
> returning a true or a false part of a condition, a more detailed
> information can be found there: http://en.wikipedia.org/wiki/%3F:
>
> *PHP and C++ implementation:* Value *=* condition *?* value if true *:*
> value if false;
> *Pascal implementation:* Value := IfThen(Condition, TruePart, FalsePart);
>
> *function IfThen(Condition: Boolean; IfTrue, IfFalse: X): X; overload;
> begin
> if Condition then Result := IfTrue else Result := IfFalse;
> end;
> *

Note that there is a tiny little difference between those two.

the ?: operator evaluates either the TruePart or FalsePart while IfThen 
will evaluate both parts first.

The following will succeed

var
   p: PInteger;
   i: Integer;
begin
   p := nil;
   i := p <> nil ? p^ : 0;
end;

The following will fail

var
   p: PInteger;
   i: Integer;
begin
   p := nil;
   i := IfThen(p <> nil, p^ , 0);
end;



More information about the fpc-devel mailing list