Recent

Author Topic: Inline If statement, i.e. IIF()?  (Read 3723 times)

1HuntnMan

  • Full Member
  • ***
  • Posts: 214
  • From Delphi 7 to Lazarus
    • NewFound Photo Art
Inline If statement, i.e. IIF()?
« on: May 19, 2024, 05:55:52 pm »
Does Lazarus/FPC have an inline IIF()? It seems like long time ago back in the 1980's with Turbo Pascal or in the early 1990's in Delphi 5 or 7 there was an IIF().  I maybe mistaken.

1HuntnMan

  • Full Member
  • ***
  • Posts: 214
  • From Delphi 7 to Lazarus
    • NewFound Photo Art
Re: Inline If statement, i.e. IIF()?
« Reply #1 on: May 19, 2024, 05:57:07 pm »
I tried to find it in Help but came of with no search.

jamie

  • Hero Member
  • *****
  • Posts: 6376
Re: Inline If statement, i.e. IIF()?
« Reply #2 on: May 19, 2024, 06:02:27 pm »
the closes I can think of is the "ifThen" function.

IIF is a $M access thing and not sure if that actually existing, I've never seen it here.
The only true wisdom is knowing you know nothing

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11654
  • FPC developer.
Re: Inline If statement, i.e. IIF()?
« Reply #3 on: May 19, 2024, 06:02:48 pm »
There are procedural workarounds ifthen() in delphi and FPC, but it evaluates all arguments(contrary to C\s "?"operator)

It might be that for some variants (e.g. strings ) you might need to include certain units (strutils in that case on at least Delphi)

« Last Edit: May 19, 2024, 07:40:51 pm by marcov »

jamie

  • Hero Member
  • *****
  • Posts: 6376
Re: Inline If statement, i.e. IIF()?
« Reply #4 on: May 19, 2024, 07:00:32 pm »
Although I don't do a lot of database stuff, here is something that may interest  you.

Code: Pascal  [Select][+][-]
  1. Function IIF(A:array of Boolean;aIsTrue,aIsFalse:Variant):Variant;
  2. Var
  3.   I:Integer;
  4.   B:Boolean;
  5. Begin
  6.   B:= False;
  7.   For I := 0 To Length(A)-1 do
  8.    Begin
  9.     B :=A[I];
  10.     IF B=False then
  11.     Break;
  12.    end;
  13. Case B of
  14.  True:Result := aIsTrue;
  15.  False:Result:= aIsFalse;
  16. end;
  17. End;
  18. procedure TForm1.Button1Click(Sender: TObject);
  19. begin
  20.  caption := IIf([1 =1,5=54, 7=7],'True','False')
  21. end;
  22.                                                  

With that, you should be able to return any  basic type since Variants are in use here.

also, you can test a series of conditions that need all to be true, not just one.


The only true wisdom is knowing you know nothing

MarkMLl

  • Hero Member
  • *****
  • Posts: 7102
Re: Inline If statement, i.e. IIF()?
« Reply #5 on: May 19, 2024, 07:11:31 pm »
There are procedural workarounds ifthen() in delphi and FPC, but it evaluates all arguments(contrary to C ?)

Sod C, that's contrary to ALGOL-60 :-(

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11654
  • FPC developer.
Re: Inline If statement, i.e. IIF()?
« Reply #6 on: May 19, 2024, 07:42:34 pm »
Sod C, that's contrary to ALGOL-60 :-(

I'm sorry, I'm only half a century old   O:-)

1HuntnMan

  • Full Member
  • ***
  • Posts: 214
  • From Delphi 7 to Lazarus
    • NewFound Photo Art
Re: Inline If statement, i.e. IIF()?
« Reply #7 on: May 19, 2024, 07:55:50 pm »
LoL! Yup, a bit old. Thanks all, I already wrote it with an IF statement but was just curious. I may take some time and try out Jamie's IIF function.

Tks...

ASerge

  • Hero Member
  • *****
  • Posts: 2288
Re: Inline If statement, i.e. IIF()?
« Reply #8 on: May 20, 2024, 05:00:53 pm »
also, you can test a series of conditions that need all to be true, not just one.
The same, but a little shorter:
Code: Pascal  [Select][+][-]
  1. function IIF(const A: array of Boolean; const AIsTrue, AIsFalse: Variant): Variant;
  2. begin
  3.   if System.IndexByte(A[Low(A)], Length(A), Ord(False)) < 0 then
  4.     Result := AIsTrue
  5.   else
  6.     Result := AIsFalse;
  7. end;
except for the case [], which is a separate topic for discussion.

Thaddy

  • Hero Member
  • *****
  • Posts: 15191
  • Censorship about opinions does not belong here.
Re: Inline If statement, i.e. IIF()?
« Reply #9 on: May 20, 2024, 06:23:33 pm »
Does Lazarus/FPC have an inline IIF()? It seems like long time ago back in the 1980's with Turbo Pascal or in the early 1990's in Delphi 5 or 7 there was an IIF().  I maybe mistaken.
There is a generic ifthen in sysutils. I do not know if it is documented yet. I wrote it, Michael committed it. Some years ago...
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. uses sysutils;
  3. var
  4.   a:integer = 12345;
  5.   b:integer = 54321;
  6. begin
  7.   writeln(specialize ifthen<integer>(b > a,a,b));
  8.   writeln(specialize ifthen<integer>(b <= a,a,b));
  9. end.
There are more ifthen's in the sources, like in math;
Note that in this case there is no shortcut boolean evaluation because the condition needs full boolean evaluation.

Note 2: there is no iif in D5 or D7 but it was common to write one.
« Last Edit: May 21, 2024, 09:30:08 am by Thaddy »
Of course the national anthem of the U.S.A. was written by Jimi Hendrix, didn't you know that?

Warfley

  • Hero Member
  • *****
  • Posts: 1513
Re: Inline If statement, i.e. IIF()?
« Reply #10 on: May 21, 2024, 09:57:06 am »
What's truly missing is not an inline if but an inline while statement:
Code: Pascal  [Select][+][-]
  1. x := WhileDo(condition, x+1);

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11654
  • FPC developer.
Re: Inline If statement, i.e. IIF()?
« Reply #11 on: May 21, 2024, 10:03:27 am »
What's truly missing is not an inline if but an inline while statement:
Code: Pascal  [Select][+][-]
  1. x := WhileDo(condition, x+1);

Anonymous methods?

Thaddy

  • Hero Member
  • *****
  • Posts: 15191
  • Censorship about opinions does not belong here.
Re: Inline If statement, i.e. IIF()?
« Reply #12 on: May 21, 2024, 02:34:06 pm »
What's truly missing is not an inline if but an inline while statement:
Code: Pascal  [Select][+][-]
  1. x := WhileDo(condition, x+1);

Anonymous methods?
That is a nice teaser to play with  8-)
Of course the national anthem of the U.S.A. was written by Jimi Hendrix, didn't you know that?

MarkMLl

  • Hero Member
  • *****
  • Posts: 7102
Re: Inline If statement, i.e. IIF()?
« Reply #13 on: May 21, 2024, 06:30:01 pm »
However whiledo()- whether or not it uses generics etc.- is a lesser problem then the non-implementation of an "inline-if" (conditional expression or whatever you want to call it).

The salient point is that a function ifthen(c, a, b) will evaluate the condition c and then both expression a and expression b, while the conditional expression supported by most ALGOL-derivatives will evaluate /either/ a /or/ b (and this is distinct from the normal consideration of boolean-expression shortcircuiting).

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Thaddy

  • Hero Member
  • *****
  • Posts: 15191
  • Censorship about opinions does not belong here.
Re: Inline If statement, i.e. IIF()?
« Reply #14 on: May 21, 2024, 07:20:56 pm »
I agree with you in sofar that both MvC -commit and helper - and me - author - (and some helpful others on the mailing list) would expect the code to use shortcut boolean evalutation if c is a constant value that does not need further evaluation if a resolves to c, assuming {$B+} state.
This seems currently not the case. OTOH the fact that c can be evaluated if it is not a constant makes the generic function still very valuable. Then again, b needs to be compiled anyway, since the function may rely also on runtime.
A small test in C shows that that behavior is the same. If c is a conditional expression the code is not optimized.
I did not test it otherwise, nor used compiler and/or linker optimizations, though.
« Last Edit: May 21, 2024, 08:17:10 pm by Thaddy »
Of course the national anthem of the U.S.A. was written by Jimi Hendrix, didn't you know that?

 

TinyPortal © 2005-2018