After reading this forum for several weeks, I am fully in opposition to removing forced unwrap from the language as it is spelled and enabled today. It is clear that even in my own usage of forced unwrap, it is used in specific cases that make my life easier and cause zero harm. For others, the feature seems used for optimization (I don't actually know if forced unwrap is quicker than if let, but it seems reasonable to assume so. In my case, I use it for three reasons:
- When debugging <--- ESPECIALLY. There have been bugs in Swift that don't report something is nil, never break on the correct line, crash and I have NO idea why. Forced unwrap solved the problem SO many times and forced the app to crash correctly.
- When I have extremely large data structures that are guaranteed to have non nil optionals after a particular flag is set. The structures and classes I design are often large and arrive at such a condition after loading lots of external resources. I have ZERO desire to write an *if let when that condition is set.
- When I have a function that needs to initialize a variable (which is optional) and then I need to return that same object. e.g
class NotEverythingIsNonNilUntilAllThingsAreLoaded {
var thing: SomeThing?
func someFunctionThatIsCalledWhenSomethingIsNeeded() -> SomeThing {
self.thing = SomeThing()
self.thing!.cat = 100 //it was just initalized, I don't need to check if self.thing not nil
return self.thing! //I'm definitely not wrapping this in an if let to find out what I already know.
}
}
This is similar to what might be written in a lazy like getter minus the check to see if it was already initialized.
You're not going to make me believe I should have used an if let to return what I just created or that I need to create another variable just to return that variable with out have to use forced unwrap.
As for people who are very excited to add messages everywhere. This example does NOT require some sort of assertion with an even more detailed explanation of why it crashed. If it crashed, it wasn't the forced unwrap that caused it, it was some fault in the OS or Runtime.
Can you imagine how many programmers are going to just put "Something happened! Pretty impossible but it did, sorry your app crashed bro." or leave the messages as "" <-- those are blank quotes.
Furthermore, the default in Swift is to not use optionals at all. Most properties of a class or structure are asked to be initialized by default-- at least that is what the language almost forces us into--. So allowing the continued existence of the forced unwrap postfix operator spelled '!' is not anything to concern about.
We've already reached the edge of where the Nanny-programming language needs to hold power.
I understand what the intent was in this proposal, but it ignores the useful cases which make it handy and hardly dangerous. It's also overkill on safety.
Swift can not be perfect, it's written in C++ for God's sake.