Kja*_*son 3 delphi delphi-2007
我正在使用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
on e: EAbort do begin
end;
end;
end;
function doo3: integer;
begin
exitIfFalse(foo(42), 1);
exitIfFalse(foo(8), 2);
Result:= 0;
end;
Run Code Online (Sandbox Code Playgroud)
小智 8
后来的Delphi版本(2009年和更新版本)接近尾声:它们让你写作
function doo3: integer;
begin
if not foo(42) then Exit(1);
if not foo(8) then Exit(2);
Result:= 0;
end;
Run Code Online (Sandbox Code Playgroud)
请注意新Exit(value)
表单如何与更传统的表单相结合Result
.
Delphi 2007不正式支持此类或类似内容.
一个完全不受支持的黑客可能对你有用:Andreas Hausladen的DLangExtensions(确保使用旧的版本)也为Delphi 2007提供了这种语法.