Does "Accept Interfaces" break deprecation tooling?

99 Views Asked by At

Deprecation

The supported way of marking functions as deprecated is something like this:

type MyStruct struct {
}

// MyFunc returns hello
// Deprecated: Use YourFunc
func (m MyStruct) MyFunc() string {
  return "hello"
}

Modern IDEs will highlight any usages of this function and linters might also raise warnings (I haven't personally checked this)

Showing the IDE highlight

Accept interfaces. Return structs.

A popular best practise is "Accept interfaces. Return structs." - which tends to encourage SOLID design in software.

However, the following code - which follows this best practise - conceals the deprecation warning:


// MyInterface specifies a single function that we require from a dependency
type MyInterface interface {
    MyFunc() string
}

func main() {

    var v MyInterface
    v = MyStruct{}
    v.MyFunc()

}

Showing the lack of IDE highlighting

Question

Is there a solution to this problem?

If I were, for example, a library maintainer: how can I ensure that my deprecation warnings are seen by users of the library who are also following best practises and defining their own dependency interfaces.

1

There are 1 best solutions below

4
Dylan Reimerink On

That seems logical since the method of the interface has not been deprecated. Adding the Deprecated: line to the interface function might help in this case (didn't test, since VSCode doesn't do this yet).

// MyInterface specifies a single function that we require from a dependency
type MyInterface interface {
    // Deprecated: use YourFunc
    MyFunc() string
}

In this case, because the interface only has 1 function you should deprecated the whole thing. Which I know is supported by godoc/pkg.go.dev, take Queryer for example.

// MyInterface specifies a single function that we require from a dependency
// Deprecated: use YourInterface
type MyInterface interface {
    MyFunc() string
}