swiftui

Can you deprecate a function so that a different overload is used by default?


I'm working on a project where there have been lots of functions written that mask the exact same interface as the vanilla SwiftUI functions.

i.e.

func padding(_ padding: OurOwnPadding) -> some View

Where OurOwnPadding here is not a SwiftUI type but has been added as a custom type to the project.

This means that when you're in a view and do .padding(.horizontal). Even though it looks like you're using the SwiftUI function, you're not. You're using this one.

We're trying to remove these functions but it would be good to not have new instances use it.

Can I mark this function in some way so that the actual vanilla SwiftUI function is picked up instead of this one? It should only pick this function up if I do something like...

let padding = OurOwnPadding.horizontal

...

.padding(padding)

Solution

  • You can mark your own padding method with @_disfavoredOverload. From the documentation,

    Use @_disfavoredOverload to work around known bugs in the overload resolution rules that cannot be immediately fixed without a source break. Don't use it to adjust overload resolution rules that are otherwise sensible but happen to produce undesirable results for your particular API

    From your description of the situation, it seems like this is a case of “overload resolution rules that cannot be immediately fixed without a source break.”