What is your evaluation of the proposal?
-1
Is the problem being addressed significant enough to warrant a change to Swift?
I don't believe so. In fact I think it will cause more problems and violates the Source Compatibility guidelines
- The new syntax/API must be clearly better and must not conflict with existing Swift syntax.
and
- There must be a reasonably automated migration path for existing code.
In the original proposal it was acknowledged that
This is a source-breaking change for try?
expressions that operate on an Optional
sub-expression if they do not explicitly flatten the optional themselves.
but then immediately followed by an assumption
It appears that those cases are rare, though;
While this might be true for some use-cases, it doesn't change the fact that it breaks this use-case:
if try? doSomething() == nil {
// ...
}
I think most would agree that this idiom is simple enough; we want to handle the failure case. Because the proposed change would cause this to behave differently only in some cases, and places where this was used would now need to reason their code if they can keep their current code or if they should migrate to
do { try doSomething() } catch {
// ...
}
It is also mentioned in the proposal that generics would still behave the way they currently do.
Generic code that uses try?
can continue to use it as always without concern for whether the generic type might be optional at runtime. No behavior is changed in this case.
This duality for me will cause a lot more harm, and will likely be sources of hard-to-find errors especially in apps that heavily rely on generics (e.g. RxSwift, etc). If we can reconcile these cases in one rule then I'd likely support a convenience change like this one.
There are also issues raised about as?
already flattening optionality, but I think that is a completely different discussion because casting implies you are trying to convert one type to another. The semantic nullness of try?
is not due to conversion, but due to the existence of an error.
Does this proposal fit well with the feel and direction of Swift?
No. I've wanted the try?
syntax way back, but I feel many are using it as a way to ignore the result altogether. Instead it should be to inline error handling when the Error
is not needed but distinguishing between failure or success is.
(Can we get a "try?" as a more leni… | Apple Developer Forums)
The purpose of 'try?' is not to allow you to ignore errors; it's to treat all errors the same way, inline. If you want to ignore the error, you have to say so explicitly. (And if there should never be an error, use 'try!'.)
I've seen some points in this thread that say the current status quo forces them to write "workarounds" or "boilerplate" code. Code is read more than it is written. In my opinion, these constructs are not "workarounds", but are there to communicate intent.
If you have used other languages or libraries with a similar feature, how do you feel that this proposal compares to those?
N/A
How much effort did you put into your review? A glance, a quick reading, or an in-depth study?
I have read the original proposal and about half of the discussion in this thread as of the time of writing. I also reviewed the original discussion during the implementation of try?
.