lazarus中模拟 IIF() 函数

admin 2月前 221191

示例1:

{$mode objfpc}
uses sysutils;
var
  a:integer = 12345;
  b:integer = 54321;
begin
  writeln(specialize ifthen<integer>(b > a,a,b));
  writeln(specialize ifthen<integer>(b <= a,a,b));
end.


示例2:

program tifthen;
 
{$mode objfpc}
 
uses
  SysUtils;
 
type
  TTest = class
    f: LongInt;
  end;
 
var
  t: TTest;
 
procedure DoTest;
begin
  { with $B+ this would crash if t is Nil }
  Writeln(specialize IfThen<LongInt>(Assigned(t) and (t.f = 42), 1, 2));
end;
 
begin
  t := Nil;
  DoTest;
  t := TTest.Create;
  DoTest;
  t.f := 42;
  DoTest;
end.


最新回复 (0)
返回