Updates to the @available attribute

So I recently ran into an issue while working on a 3rd party library that is essentially a wrapper around the Foundation’s Process class. Multiple functions in Process are currently not implemented in the Linux Foundation library and I was trying to mark the implementations of these functions as unavailable for linux by doing this:

@available(Linux, unavailable, message: "The terminate() function is not yet implemented on linux")
public func terminate() {
    self.process.terminate()
}

Unfortunately, this did nothing when attempting to use the function on linux and so I was forced to do this instead:

#if os(Linux)
@available(Linux, unavailable, message: "The terminate() function is not yet implemented on linux")
public func terminate() {}
#else
public func terminate() {
    self.process.terminate()
}
#endif

We wanted to give Linux users a compile-time error message when trying to use the function rather than just flat-out not have the function at all and this workaround is very ugly. I tried to do this:

#if os(Linux)
@available(Linux, unavailable, message: "The terminate() function is not yet implemented on linux")
#else
public func terminate() {
    self.process.terminate()
}
#endif

but it wouldn’t compile because the @available attribute has to immediately precede the function.

Would it be possible to add a Linux flag to @available()? There’s already flags for swift and for each of the Apple platforms. If there’s a compiler flag for the Linux OS, why isn’t there an @available flag for it too?

I’m afraid I don’t know enough about the swift language backend to even know where to look for the @available code, let alone implement it myself. I am happy and willing to learn though if someone could give me push in the right direction.

2 Likes

I want to revive this thread.
It would be amazing if we could use at least all official supported platforms in the available attribute.

Currently we could have maybe these additional ones:

  • linux
  • ubuntu 16.04, ubuntu 18.04, ubuntu 20.04
  • centOS 7, centOS 8
  • amazonLinux 2
  • windows 10

Is there anything, that does speak against this addition? Otherwise I would open a pitch thread soon.

1 Like

If you’re trying to solicit comments from the community as broadly as possible, best not to revive a very old thread but start a new one.

Yeah. As I said, I will create a pitch thread. I don't think that this feature would need much pre-discussion.

Just so that nobody is confused by me not making a pitch thread, I want to say that I could just use #if canImport() for my specific use case.
If someone else has examples, where #if canImport() does not work or is very inconvenient, please start a thread yourselves and I will support you however I can (because I still think, that this is a big gap in Swift's consistency).

Terms of Service

Privacy Policy

Cookie Policy