我想将一个方法标记为过时,但 Delphi 5 没有这样的功能。
举个例子,这里是一个虚构的方法,它已被弃用和新的首选形式:
procedure TStormPeaksQuest.BlowHodirsHorn; overload; //obsolete
procedure TStormPeaksQuest.BlowHodirsHorn(UseProtection: Boolean); overload;
注意:对于这个假设的例子,我们假设使用无参数版本是非常糟糕的。不“使用保护”存在问题 - 没有好的解决方案。没有人喜欢不得不使用保护,但没有人愿意不使用保护。所以我们让调用者在吹霍迪尔的喇叭时决定他们是否要使用保护。如果我们默认无参数版本继续不使用保护:
procedure TStormPeaksQuest.BlowHodirsHorn;
begin
BlowHodirsHorn(False); //No protection. Bad!
end;
那么开发人员就有各种讨厌的东西的风险。如果我们强制无参数版本使用保护:
procedure TStormPeaksQuest.BlowHodirsHorn;
begin
BlowHodirsHorn(True); //Use protection; crash if there isn't any
end;
如果开发商没有得到任何保护,或者没有任何保护,那么就有可能出现问题。
现在我可以重命名过时的方法:
procedure TStormPeaksQuest.BlowHodirsHorn_Deprecatedd; overload; //obsolete
procedure TStormPeaksQuest.BlowHodirsHorn(UseProtection: Boolean); overload;
但这会导致编译错误,人们会嘲笑我(我真的不想听到他们的抱怨)。我希望他们得到一个唠叨,而不是一个实际的错误。
我想过添加一个断言:
procedure TStormPeaksQuest.BlowHodirsHorn; //obsolete
begin
Assert(false, 'TStormPeaksQuest.BlowHodirsHorn is deprecated. Use BlowHodirsHorn(Boolean)');
...
end;
但我不能保证开发人员不会在没有断言的情况下发布版本,从而给客户造成严重的崩溃。
如果开发人员正在调试,我考虑只使用抛出一个断言:
procedure TStormPeaksQuest.BlowHodirsHorn; //obsolete
begin
if DebugHook > 0 then
Assert(false, 'TStormPeaksQuest.BlowHodirsHorn is deprecated. Use BlowHodirsHorn(Boolean)');
...
end;
但我真的不想造成崩溃。
如果它们在调试器中,我想显示一个 MessageDlg(这是我过去做过的一种技术):
procedure TStormPeaksQuest.BlowHodirsHorn; //obsolete
begin
if DebugHook > 0 then
MessageDlg('TStormPeaksQuest.BlowHodirsHorn is deprecated. Use BlowHodirsHorn(Boolean)', mtWarning, [mbOk], 0);
...
end;
但这仍然太具有破坏性。并且它导致了代码卡在显示模式对话框的问题,但对话框并不明显可见。
我希望有某种警告信息会坐在那里唠叨他们 - 直到他们挖出他们的眼睛并最终改变他们的代码。
我想也许如果我添加了一个未使用的变量:
procedure TStormPeaksQuest.BlowHodirsHorn; //obsolete
var
ThisMethodIsObsolete: Boolean;
begin
...
end;
我希望这只会在有人引用代码时引起提示。但是即使您不调用实际使用过时的方法,Delphi 也会显示提示。
谁能想到别的?