[fpc-pascal] New feature: IfThen() intrinsic

Michael Van Canneyt michael at freepascal.org
Tue Feb 2 10:44:08 CET 2016



On Tue, 2 Feb 2016, Graeme Geldenhuys wrote:

> On 2016-02-02 09:07, Michael Van Canneyt wrote:
>> The "if then" construct is really confusing.
>> "if then" is a statement, not an expression.
>
> Exactly! I'm glad somebody noticed that too.
>
> I'm fine with the iif() syntax. It is used in a few languages for many
> years, so is a common and known name.
>
> Then again, I really don't know what the issue is here, because it is so
> simple to implement your own in Object Pascal. For example, the tiOPF's
> tiUtils.pas unit has had it for over 15 years now.

Try this (A a component or class with property Name):

N:=iif(Assigned(A),A.Name,'Nil');

You'll get a crash using regular functions because All arguments are
evaluated prior to calling iif().

If IIF() is an intrinsic, then the compiler will only evaluate the argument
that corresponds to the condition.

Michael.



More information about the fpc-pascal mailing list