我知道我可以使用以下构造检查 Delphi 的 switch 指令的当前状态:
{$IFOPT R+}
Writeln('Compiled with range-checking');
{$ENDIF}
由于我缺乏关于 Delphi 后端编译器如何工作的深入资源,我不确定是否有一种方法可以根据调用它的代码行中 switch 指令的状态来改变函数的行为。它看起来像这样:
procedure P1;
begin
{$I+}
P3;
{$I-}
end;
// ** state of I unknown
procedure P2;
begin
{$I-}
P3;
{$I+}
end;
// ** state of I unknown
procedure P3;
begin
// Something like {$IFOPT I+}, but at the state P3 is called
DoThis;
{$ELSE}
DoThat
{$ENDIF}
end;
我正在为我迫切希望保持不变的遗留代码编写适配器。P3 不需要使用指令,但我认为这是要走的路。