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

Jer*_*dge 2 delphi interface deprecated delphi-10-seattle

我有一个接口和一个相应的接口对象.在这些函数中,我将其中一个标记为deprecatedinterface和interfaced对象:

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

  TSomeInterface = class(TInterfacedObject, ISomeInterface)
    function SomeFunctionName: String; deprecated;
  end;
Run Code Online (Sandbox Code Playgroud)

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

符号'SomeFunctionName'已弃用

这来自接口对象的定义,它说的是TSomeInterface = class( ....如果我deprecated从interfaced对象中删除,则警告消失.但我仍然希望在该功能上标记这一点.

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

Del*_*ics 7

确实存在编译器错误(或至少奇怪定义的行为).但是,我认为你可以实现你想要的而不会遇到这个问题.

编译器问题似乎是将接口方法和该方法的实现标记为deprecated不推荐警告的结果,即使该接口方法从未实际使用过.

但是你不需要标记两者.

将您的接口方法标记为已弃用,并保留该方法的实现未标记.我还强烈建议您将接口方法设为私有或至少保护,以避免通过对象引用访问它们.

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

如果您的代码通过接口引用引用了不推荐使用的方法,则只会收到警告:

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;
Run Code Online (Sandbox Code Playgroud)