我正在使用Delphi 2007,并且想知道以下是否可行,如果不是,那么它可能在另一个版本的Delphi中.
我的代码目前看起来像,doo1但我想拥有的是类似的东西doo3.
我已经制作doo2并且它有效但我更喜欢exitIfFalse在一个地方使用该功能而不是在许多地方作为子程序.
function foo(const bar: Word): boolean;
begin
Result:= bar = 42;
end;
function doo1: integer;
begin
if not foo(42) then begin
Result:= 1;
exit;
end;
if not foo(8) then begin
Result:= 2;
exit;
end;
Result:= 0;
end;
function doo2: integer;
Procedure exitIfFalse(const AResult: boolean; const AResultCode: integer);
begin
if not AResult then begin
Result:= AResultCode;
Abort;
end;
end;
begin
Result:= -1;
try
exitIfFalse(foo(42), 1);
exitIfFalse(foo(8), 2);
Result:= 0;
except …Run Code Online (Sandbox Code Playgroud)