afero icon indicating copy to clipboard operation
afero copied to clipboard

Make constructors return structs instead of interfaces

Open jribe opened this issue 7 years ago • 2 comments
trafficstars

This changes the return type of the various NewXxxx functions from Fs to the relevant type that's being returned.

To maintain the same level of compile-time checking, each type impacted now has a var _ Fs = (*TheType)(nil) no-op assignment to assert that the type implements Fs.

This change brings afero more in line with the "accept interfaces, return structs" idiom.

Importantly, this makes the constructor functions easier to discover in GoDoc since they will now be grouped with the structs that they build.

AFAIK, this should be completely backwards compatible despite changing the API because:

  • The values are still assignable to Fs
  • Interfaces are transparent to reflection

Edit: I suppose this would be a breaking change if anybody assigned the constructor to a value. Seems unlikely, but plausible. Would that require a major version bump?

Edit 2: This also breaks any code that performs a type assertion on the returned value (by making the assertion unnecessary).

Fixes https://github.com/spf13/afero/issues/76

jribe avatar Nov 19 '18 19:11 jribe

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar Nov 19 '18 19:11 CLAassistant

Be great to see this land

jeffwidman avatar Jul 14 '20 06:07 jeffwidman