How do you properly deprecate a method in a Protocol?

Without using @objc @optional, is there a way to properly deprecate a function in a protocol? I cannot make use of the Objective-C runtime because I tend to have struct types conforming to this protocol.

Trivialized example:

protocol P {
    @available(*, deprecated: 1.0.1, renamed: "g")
    func f(x: Int)

    func g(x: Int, y: String)

struct S: P { // Complains that "Type 'S' does not conform to Protocol 'P'"

    func g(x: Int, y: String) {



Obviously, I could just remove the method, however, I'd like to notify the user that the method was renamed and allow the compiler to offer a fix.

Thanks for your time.

You could add a default implementation on the protocol that does nothing.

Right, I could do that, but that doesn't inform consumers of previous versions of my library that a method was deprecated.

No, but the fact that you've added a new requirement to the protocol is going to do that as well, since they'll suddenly get an error about a new requirement, which they'll have to lookup.

I believe it would be an IDE feature to warn users they're implementing a deprecated method. I'm not sure if that deserves its own language feature. I believe Xcode already shows such methods as being crossed out.

Right, they'll be able to get the new requirement, but they won't no the other one was deprecated and renamed to to this new one without looking at the method definition.

My hope is that it would be a feature of the compiler or IDE to error+correct the user like it does for classes and structs.

Swift would need a way for the method in the conforming type to be marked so it's clear it's fulfilling a protocol requirement. This has been discussed in the past here:

Wow! It looks like it's been discussed on multiple occasions, but never to any real solid conclusion.

Thanks, @lancep!

Do you have any personal recommendations on how you feel this should be done?

Terms of Service

Privacy Policy

Cookie Policy