Introduce `#if stdlib` or clarify `#if swift`

Yes, the #if canImport(Swift, _version: 5.7) syntax should work today as a solution to the problem. It would be relatively low effort to turn it into an official feature with operator syntax replacing the _version: argument and I think there have been good arguments made in this thread for doing so:

  • It solves a real problem that comes up repeatedly.
  • The design is flexible; it's not overly specific to either the standard library or Apple's SDK distributions.

The main problem I see with the design is that it is not very straightforward to discover the version number you ought to use in order to detect the condition you're interested in. The standard library follows a versioning convention that makes this easier but the versioning of other modules can be somewhat arbitrary. You may need to inspect the .swiftinterface distributed with the library to check the version you have currently or ask the library owner to tell you which version to check in order to solve your problem. I personally think that the utility of the feature outweighs the awkwardness, though, and I'd rather we made this simple extension of canImport available sooner rather than wait for a more ambitious, more ergonomic design to be worked out (which wouldn't be precluded by exposing this functionality now).

6 Likes