A strong -1.
No.
To elaborate:
Examples of Real-World Use
None of these use cases are able to convince me:
let event = NSApp.currentEvent!
This code clearly states that there should be an event. If the unwrap does fail, it's clearly because there's no event (for some unknown reason).
let event = NSApp.currentEvent !! "Trying to get current event for right click, but there's no event"
How is this any better? Only the obvious is stated here, but there is no clue as to the reason why there's no event.
let existing = childViewControllers as! Array<TableRowViewController>
This code clearly states that the childViewControllers
should be TableRowViewController
s.
let existing = childViewControllers as? Array<TableRowViewController> !! "TableViewController must only have TableRowViewControllers as children"
Again, this only states the obvious, as do all the other examples in the proposal.
IMHO, the use of !!
here offers no additional value. In fact, I would actively remove it from my projects.
Encourages a thoughtful approach to unwrapping,
I don't agree here, for two reasons:
- Force unwrap should only be used when an optional can not or should not be
nil
. It should be obvious from the immediately surrounding code that this is the case. If it isn't obvious, then there's a high risk of shooting yourself in the foot and you're better off with a properguard
than trying to explain to yourself (or others) why it shouldn't benil
. - Just like force unwrap is misused by lazy (or beginning) programmers, so too will
!!
become a lazy (wo)man's form of error handling. I feel an explicitguard
encourages more thought than!!
, as it offers options. Perhaps the error isn't really an error after all and you can just log a warning and return? Perhaps the error is recoverable, should be handled by the caller and you can use throw instead? When all else fails, there's stillfatalError
.
Provides a succinct and easily-taught form for new Swift learners.
As someone whose job it is to teach new learners, I can say with lots of confidence that this won't be the case. As explained above, new learners will misuse !!
as a way to quickly print an error message, ignoring proper (thoughtful) error handling all together.
No. As explained above, I don't feel like this proposal warrants a new operator and even risks achieving the opposite of what it's trying to do.
I've read the proposal and reviewed some of my projects to look for areas where this operator could add value. I found none.