Jerry Dodge
Jerry Dodge

Reputation: 27276

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

I have an interface, and a corresponding interfaced object. Among the functions, one of them I have marked as deprecated in both the interface and interfaced object:

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

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

However, upon compilation, I receive a compiler warning:

Symbol 'SomeFunctionName' is deprecated

This comes from the definition of the interfaced object, where it says TSomeInterface = class( .... If I remove deprecated from the interfaced object, the warning goes away. But I still do want this to be flagged on that function.

What do I need to do to stop this compiler warning while still flagging this as deprecated? Should I only flag it on the interface instead of both? Or is there a way to instruct the compiler not to raise a warning?

Upvotes: 2

Views: 547

Answers (1)

Deltics
Deltics

Reputation: 23036

There does appear to be a compiler bug (or at least strangely defined behaviour). However, I think you can achieve what you want without running into that problem.

The compiler problem appears to be that having both an interface method and the implementation of that method marked as deprecated results in the deprecated warning even if that interface method is never actually consumed.

But you do not need to mark both.

Mark your interface method as deprecated and leave the implementation of that method unmarked. I would also strongly recommend that you make your interface methods private or at least protected to avoid the possibility of accessing them through object references.

The implementation of the method satisfies the contract that the interface must be implemented. The deprecated status of the method on the interface is then important only for any code that is consuming the method through that interface.

You will then only receive a warning if you have code that references the deprecated method through the interface reference:

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;

Upvotes: 7

Related Questions