Sure it can:
var widgetsAreAvailable: Bool {
if #available(macOS 11, iOS 14, *) { return true }
else { return false }
}
if !(widgetsAreAvailable) {
widgetsUnavailableLabel.hidden = true
}
But perhaps you mean that this can't accurately do the right thing for future platforms here, and that's true. But it's already the existing case. (Really, the right solution in this particular case is if #canImport(WidgetKit)
, but that only works because widgets have been defined in a separate module.)
If this is really meant to be the inverse of #available
, then it seems like there should not be any *
. If we could write out the "else" condition that #unavailable
is mimicking, I think it would look like this:
if #available(iOS 14, macOS 11, *) {
doNewStuff()
}
else { // #unavailable(iOS 14, macOS 11)
doOldStuff()
}
Note that the *
still lives on the top line; it doesn't need to appear on both conditions, because the wildcard has already caught everything else.
I'm honestly not sure what the right solution is here. I think #unavailable(*)
is confusing, and I don't think we should accept the proposal as-is. The natural solution in my mind is to disallow the *
, but as @xwu points out, users may be confused that this code doesn't run on linux:
if #unavailable(iOS 14) {
// linux: iOS 14 is *clearly* unavailable, so why doesn't this run?
}
I think that situation is less confusing than the one where a *
would explicitly suggest to me that it should run on any platform, but I won't argue that it's not at all confusing. We can add documentation all we want, but as you yourself demonstrated, users may not find that documentation even when explicitly looking for it.
Perhaps we could issue a warning if compiling on non-mentioned platforms, with an accompanying fixit?
// warning: on 'linux' platform, this condition will be false.
// fixit: change to `#unavailable(iOS 14, linux *)` to silence this warning
if #unavailable(iOS 14) { }
(edit: Yes, I invented the linux *
syntax here, and I'm not convinced it's right either. But some callout of linux would be appropriate. Maybe linux 0
instead?)