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.