如何获得已弃用的接口函数以停止显示编译器警告?

How to get deprecated interface function to stop showing compiler warning?

我有一个接口,以及一个相应的接口对象。在函数中,其中一个我在接口和接口对象中都标记为deprecated

type
  ISomeInterface = interface
    function SomeFunctionName: String; deprecated;
  end;

  TSomeInterface = class(TInterfacedObject, ISomeInterface)
    function SomeFunctionName: String; deprecated;
  end;

但是,在编译时,我收到编译器警告:

Symbol 'SomeFunctionName' is deprecated

这来自接口对象的定义,其中写着 TSomeInterface = class( ...。如果我从接口对象中删除 deprecated,警告就会消失。但我仍然希望在该功能上标记它。

我需要做什么才能停止此编译器警告,同时仍将其标记为 deprecated?我应该只在界面上标记它而不是两者都标记吗?或者有没有办法指示编译器不发出警告?

似乎确实存在编译器错误(或者至少是奇怪定义的行为)。但是,我认为您可以在不 运行 解决该问题的情况下实现您想要的。

编译器问题似乎是同时具有 接口 方法和该方法的 实现 标记为 deprecated即使从未实际使用该接口方法,也会导致弃用警告。

但是您不需要同时标记两者。

将您的 接口 方法标记为已弃用,并保留该方法的 实现 未标记。我还强烈建议您将接口方法设置为 private 或至少 protected 以避免通过对象引用访问它们的可能性。

方法的实现满足接口必须实现的约定。接口上方法的弃用状态仅对通过该接口使用该方法的任何代码很重要。

只有当您的代码通过接口引用引用了已弃用的方法时,您才会收到警告:

type
  ISomeInterface = interface
    function SomeFunctionName: String; deprecated;
    procedure OtherProc;
  end;

  TSomeInterface = class(TInterfacedObject, ISomeInterface)
  private // ISomeInterface
    function SomeFunctionName: String;
    procedure OtherProc;
  end;


// ..

var
  foo: ISomeInterface;
begin
  foo := TSomeInterface.Create;
  foo.SomeFunctionName;  // Will emit the deprecated warning
  foo.OtherProc;        // Will emit no warning
end;