Extend `@available` attribute to reference Package versions

Inspired by @nashysolutions looking for how to do this, I thought someone should Pitch it, so here goes:

Currently there’s no simple way to gatekeep availability for parts of your API based on versions of Package dependencies like the @available attribute allows for specifying OS versions. It seems the most obvious solution is to just make @available work for packages as well as OS versions.

Example:

@available(MyAwesomePackage, introduced: 2.0)
func newInVersionTwo() {…}

Is there any reason this couldn’t happen?

9 Likes

For the most part, I think it's relatively straight forward - but it will probably need a compiler implementation that provides the ability to define these values in the build command.

However, I do think there are some edge cases that still need to be thought through:

  1. How should dependencies pinned to branches or specific commits should be handled?
  2. Would you be referencing the package name, as defined in the manifest, or by how SwiftPM's dependency tree handles them?

I would expect it to be tied to the semantic versioning, but I guess that still leaves some unknowns when you’re trying to use “unreleased” packages.

I didn’t know there was a difference. How does the dependency tree do it?