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

Martin fpc at mfriebe.de
Tue Feb 2 16:10:47 CET 2016


On 02/02/2016 13:24, Sven Barth wrote:
>
> > Look at the "iif" as an statement and the things will be simple.
>
> BECAUSE. IT. IS. *NOT*. A. STATEMENT!
> It never was and it never will be! It returns a value and thus by 
> definition it is an expression!
>
>
+1
And imho that why it is important that it does not look like a statement 
either, i.e. it does not start with a keyword.

also if we are discussing (are we?) a form like
   x:= iIf a>b  iThen c iElse b;
then why not
   x:=  a>b  ifThen c ifElse b;

What are the advantages/disadvantages they have over each other?

They both need operator precedence:
   x :=  iIf a Then b else c + 3;
should be which
   x :=  (iIf a Then b else c) + 3;
   x :=   iIf a Then b else (c + 3);

unless a closing "end" is enforced
   x:= iIf a>b  iThen c iElse b iEnd;
Which is what the function stile does implicitly, by having brackets 
around all 3 arguments.

------------------
Btw, just for fun, did some playing to see how much could be done with 
current fpc

program Project1;
{$mode objfpc}
{$MACRO on}
{$define ifthen:=and}
{$define ifelse:=or}

type TIfThen = record
   b:boolean;
   v:integer;
end;
operator and (a:boolean;b:integer):TIfThen;
begin
   result.b := a;
   result.v := b;
end;
operator or (a:TIfThen;b:integer):integer;
begin
   if a.b then
     result := a.v
   else
     result := b;
end;

begin
   writeln( (1<2) and 3 or 4 );
   writeln( (1>2) and 3 or 4 );
   writeln( (1<2) ifthen 3 ifelse 4 );
   writeln( (1>2) ifthen 3 ifelse 4 );
   readln;
end.







More information about the fpc-pascal mailing list