[swift-evolution] [Discussion]: Deprecate !-Unwrapping of Optionals

Radosław Pietruszewski radexpl at gmail.com
Mon Feb 29 06:21:15 CST 2016


So:

a) I agree force-unwrapping is often overused and abused, especially by Swift beginners
b) I also agree with Brent that it’s a useful feature, and while not terribly often used, by me at least, important to have

Given that, I agree that having a simple to use `!` postfix operator might push people to abuse it. If it’s so simple to use, it means it’s important and should be used often. If it was harder to reach, it wouldn’t be used as often, because it would be relatively easier to do the right thing. 

But. While `!` is perhaps too easy to type, it’s also very easy to spot while looking at code. It shouts at you “hey, do you really need me here?!”. `unsafeUnwrap` is harder to spot.

Also: `!` can be chained nicely like so:

  5> struct A {
  6.     var x: Int
  7. }
  8> struct B {
  9.     var a: A?
 10. }
 11> var a = B(a: A(x: 10))
 12> a.a!.x = 20

You can’t do that with a function or a method. Though you could do it with a property, I think, like `a.a.forceUnwrapped.x = 20`.

— Radek

> On 28 Feb 2016, at 20:53, Developer via swift-evolution <swift-evolution at swift.org> wrote:
> 
> Optional values pervade Swift code and so there is a significant part of the language dedicated to manipulating them and using them safely.  An optional value may be conditionally let-bound, guarded, pattern-matched on, given a default value with ??, or used with higher-order functions like map and flatMap without having to interact with partiality.  A corner case, however, remains in the form of the postfix-! force-unwrap operator.  At the term level, there is little reason to unwrap given the syntactic constructs above, and indeed code that does becomes brittle and, by its very nature, open to the dreaded "unexpectedly found nil while unwrapping an optional value" error (our very own NullPointerException).  In addition, bang is incredibly hard to spot in code bases that use it because it blends in with Boolean negation (which, in itself, is notoriously hard to spot in other C-like-languages), harming readability and making code review more difficult.  Finally, if the authors of a piece of code have such confidence in the existence of a value that they feel it safe to use force unwrapping, they should simply use a non-optional value and cut out a level of indirection and avoid the dangers of bang in the first place. 
> 
> Because of the above, I'd like to start a discussion about its deprecation and eventual removal in favor of either an unsafeUnwrap function or other extra-syntactic constructs.  I don't intend for force-unwrapping as a concept to go away, as there will always be corner cases where it is necessary, but I don't believe it deserves to be so easy to do in a language that holds safety as a core tenant.
> 
> Thanks all,
> 
> ~Robert Widmann
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution



More information about the swift-evolution mailing list