I'd like to start saying I'm quite late to this proposal and, while I've read it, I've only taken a quick glance to the forum thread. However, I'd like to take the opportunity to comment on this proposal before it gets approved/rejected. I apologize if this has already been expressed beforehand.
I feel this proposal has good value. Checking for availability is something we developers often do on Apple platforms. However, I also feel it only solves this problem for a specific use case: availability checks.
So I found myself asking the question: What about other conditions?
I would like to propose a different solution. I believe this would be a great example of a good use case for an unless
control flow expression.
The following three examples are the same:
// if NOT in iOS 13, load the window.
// Post iOS 13 the window is loaded later in the lifecycle, in the SceneDelegate.
if #available(iOS 13, *) {
} else {
loadMainWindow()
}
guard #available(iOS 13, *) else {
loadMainWindow()
return // <-- mandatory return
}
// no-op
unless #available(iOS 13, *) {
loadMainWindow()
}
// continue flow
Contrary to guard
, unless
would not require else
nor require the flow to return
or exit.
I'm aware unless
has been suggested/pitched before [1, 2], even some suggested renaming guard
to unless
(which I would disagree with).
unless
could also be implemented as a trailing expression. While this syntax is common in other languages, might not be welcomed by Swift developers:
loadMainWindow() unless #available(iOS 13, *)
In my opinion, it would make little sense to approve a proposal for a specific case like availability checks, and not a proposal for testing any negative condition with unless
.