# Unwrap or Throw - Make the Safe Choice Easier

**URL:** <https://forums.swift.org/t/unwrap-or-throw-make-the-safe-choice-easier/14453>\
**Category:** Pitches\
**Created:** [July 14, 2018, 3:13am UTC](https://forums.swift.org/t/unwrap-or-throw-make-the-safe-choice-easier/14453 "2018-07-14T03:13:33Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![possen](https://avatars.discourse-cdn.com/v4/letter/p/ed8c4c/32.png) [@possen](https://forums.swift.org/u/possen)\
**Post date:** [July 14, 2018, 3:13am UTC](https://forums.swift.org/t/unwrap-or-throw-make-the-safe-choice-easier/14453/1 "2018-07-14T03:13:33Z")

</div>

The `!!` unwrap or die discussion, pointed out there is another way to handle optionals. Right now we have no language support to `throw` if the item is `nil`. ~~this would be preferable than only having `!!` for the following reasons (although would not reduce the usefulness of that when you don't want to make your method throwable):~~

- A single line solution, rather than `guard let` or `if let` or `if value != nil`
- Makes it easier to do the right thing, that is, write safe code, by handling the `nil` rather than force unwrapping or ignoring it.
- Completes the matrix of your choices when dealing with an optional.
- Users can consolidate common error scenarios by implementing `Error` No need for repeating the same string in multiple places. They would use a shared `enum` thus making for cleaner code
- In terms of code size, the happy path would not be dwarfed, by the error path.
- It utilizes existing `try catch throw` mechanisms.

It is possible to do without an operator by creating a `Thrower` `struct`, you can see in the first example. This is a generic class which just throws the error, admittedly not convenient and wordy. So it seems that a language change is necessary (unless others have better approaches). The examples after that show alternatives for a possible syntax to support such a feature.

> ```
> enum Errors: Error {
> case falseResult
> }
> 
> func throwIfNil(value: Int?) throws {
> try value ?? Thrower().throw(value, Errors.falseResult) // works in Swift today
> try value ?? throw Errors.falseResult // proposed
> }
> 
> struct Thrower<T> {
> // need this to return same type as value, but not used
> func `throw`(_ item: T, _ error: Error) throws -> T {
> throw error
> }
> }
> 
> ```

` edit - removed other syntaxes that I no longer think are relevant.`

I don't think we should use `?!` or similar because `!` should always mean unsafe (unless it is a `not` operator). This will be a safe operation.

The main idea is to eliminate most uses of force unwrap, and make it almost as easy to write safe code where you have nil coalescing,` guard let` and `if let` as your complete suite of choices.

Would love to get some feedback or discussion of whether this is.a interesting idea.

---

<div class="post-metadata">

**Author:** ![ahti](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ahti/32/604_2.png) [@ahti](https://forums.swift.org/u/ahti)\
**Post date:** [July 14, 2018, 3:31am UTC](https://forums.swift.org/t/unwrap-or-throw-make-the-safe-choice-easier/14453/2 "2018-07-14T03:31:06Z")

</div>

I use this in basically all projects:

```swift
struct NilError: Error {}

extension Optional {
    func unwrap(or error: @autoclosure () -> Error = NilError()) throws -> Wrapped {
        switch self {
        case .some(let w): return w
        case .none: throw error()
        }
    }
}

```

I've been quite happy with this. In some places a little sugar might be nice, in others the chainability of `try lol.unwrap(or: SendHelp()).something()` would still win.

edit: Looking at this piece of code again, I might actually change `NilError` like this, to give pretty great diagnosability, even when no custom error is provided:

> **first revision, see below**
>
> ```swift
> struct NilError: Error {
> let file: String
> let line: Int
> let column: Int
> let function: String
>     
> init(file: String = #file, line: Int = #line, column: Int = #column, function: String = #function) {
> self.file = file
> self.line = line
> self.column = column
> self.function = function
> }
> }
> 
> ```

edit 2: scratch that, #file, #line, ... don't currently seem to propagate through multiple levels of default arguments, so this is what I use now:

```
struct NilError: Error {
    let file: String
    let line: Int
    let column: Int
    let function: String
}

extension Optional {
    func unwrap(file: String = #file, line: Int = #line, column: Int = #column, function: String = #function) throws -> Wrapped {
        return try unwrap(or: NilError(file: file, line: line, column: column, function: function))
    }

    func unwrap(or error: @autoclosure () -> Error) throws -> Wrapped {
        switch self {
        case .some(let w): return w
        case .none: throw error()
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![Joe\_Groff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joe_groff/32/1929_2.png) [@Joe\_Groff](https://forums.swift.org/u/Joe_Groff)\
**Post date:** [July 14, 2018, 4:11am UTC](https://forums.swift.org/t/unwrap-or-throw-make-the-safe-choice-easier/14453/3 "2018-07-14T04:11:13Z")

</div>

If we go the route of `Never`-as-universal-subtype, then it'd be reasonable to also make `throw` an expression of type `Never`, which would let you write things like `x ?? throw error`, as in C++, Kotlin, and other languages where `throw` is an expression.

---

<div class="post-metadata">

**Author:** ![possen](https://avatars.discourse-cdn.com/v4/letter/p/ed8c4c/32.png) [@possen](https://forums.swift.org/u/possen)\
**Post date:** [July 14, 2018, 4:11am UTC](https://forums.swift.org/t/unwrap-or-throw-make-the-safe-choice-easier/14453/4 "2018-07-14T04:11:55Z")

</div>

Interesting, this is similar to the `Unwrap or Die` discussion, Chris and others suggested the `unwrap` extension. as an alternative to `!!` except you are throwing an exception instead of failing. This will only throw, the one exception, but maybe that is sufficient with the line and file name. I agree sugar would be nice here.

---

<div class="post-metadata">

**Author:** ![possen](https://avatars.discourse-cdn.com/v4/letter/p/ed8c4c/32.png) [@possen](https://forums.swift.org/u/possen)\
**Post date:** [July 14, 2018, 4:14am UTC](https://forums.swift.org/t/unwrap-or-throw-make-the-safe-choice-easier/14453/5 "2018-07-14T04:14:24Z")

</div>

Yes, thank you for expressing it. Throw needs to be an expression and the `Never` support would be perfect for that. Sounding like this idea might not be very controversial. :-)

---

<div class="post-metadata">

**Author:** ![possen](https://avatars.discourse-cdn.com/v4/letter/p/ed8c4c/32.png) [@possen](https://forums.swift.org/u/possen)\
**Post date:** [July 14, 2018, 9:58pm UTC](https://forums.swift.org/t/unwrap-or-throw-make-the-safe-choice-easier/14453/6 "2018-07-14T21:58:32Z")

</div>

It looks like `Never` is moving forward so it looks like this should be very easy to implement with that support. I suggest we that we make `throw` an expression and it implements `Never` as Joe mentions above. Does anyone have any suggestions or improvements which would enable the following? And then making a sample implementation if it looks like a good direction?

> ```
> enum Errors: Error {
> case falseResult
> }
> 
> func throwIfNil(value: Int?) throws {
> try value ?? throw Errors.falseResult     
> }
> 
> ```

---

<div class="post-metadata">

**Author:** ![Joe\_Groff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joe_groff/32/1929_2.png) [@Joe\_Groff](https://forums.swift.org/u/Joe_Groff)\
**Post date:** [July 16, 2018, 6:33pm UTC](https://forums.swift.org/t/unwrap-or-throw-make-the-safe-choice-easier/14453/7 "2018-07-16T18:33:24Z")

</div>

You could play with the idiom by putting a simple wrapper function around `throw` today:

```swift
// could be -> Never when Never-as-bottom is implemented
func raise<T>(_ error: Error) throws -> T {
  throw error
}

try value ?? raise(Errors.falseResult)

```

We originally decided against making `throw` an expression because it would be inconsistent with other control flow statements like `break`, `continue`, and `return`. With `Never` in the type system, and `rethrows` autoclosures, we could conceivably make it so that `break`, `continue`, and `return` were also expressions, and plumb them through autoclosures as if they were thrown errors so that they can branch out to the right places in the outer function. This would allow `??` to be used as shorthand for a lot of trivial one-line `guard let`s that unwrap an optional or immediately exit scope on nil.

---

<div class="post-metadata">

**Author:** ![Chris\_Lattner3](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/chris_lattner3/32/348_2.png) [@Chris\_Lattner3](https://forums.swift.org/u/Chris_Lattner3)\
**Post date:** [July 16, 2018, 6:35pm UTC](https://forums.swift.org/t/unwrap-or-throw-make-the-safe-choice-easier/14453/8 "2018-07-16T18:35:35Z")

</div>

> [@Joe\_Groff](#):
>
> If we go the route of `Never` -as-universal-subtype, then it'd be reasonable to also make `throw` an expression of type `Never` , which would let you write things like `x ?? throw error` , as in C++, Kotlin, and other languages where `throw` is an expression.

+1

-Chris

---

<div class="post-metadata">

**Author:** ![Chris\_Lattner3](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/chris_lattner3/32/348_2.png) [@Chris\_Lattner3](https://forums.swift.org/u/Chris_Lattner3)\
**Post date:** [July 16, 2018, 6:38pm UTC](https://forums.swift.org/t/unwrap-or-throw-make-the-safe-choice-easier/14453/9 "2018-07-16T18:38:44Z")

</div>

> [@Joe\_Groff](#):
>
> We originally decided against making `throw` an expression because it would be inconsistent with other control flow statements like `break` , `continue` , and `return` .

That was part of it, but we also didn't make throw an expression because we didn't have `Never`, and it wasn't a bottom type, so doing this wouldn't have had any value.

> [@Joe\_Groff](#):
>
> With `Never` in the type system, and `rethrows` autoclosures, we could conceivably make it so that `break` , `continue` , and `return` were also expressions, and plumb them through autoclosures as if they were thrown errors so that they can branch out to the right places in the outer function. This would allow `??` to be used as shorthand for a lot of trivial one-line `guard let` s that unwrap an optional or immediately exit scope on nil.

It would be very interesting to do this, I'd (tentatively) love to see it. I don't see any reason to limit it to autoclosures though, normal closures could participate as well. We'd just have to have some sort of attribute or other indicator on the closure function type.

-Chris

---

<div class="post-metadata">

**Author:** ![Joe\_Groff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joe_groff/32/1929_2.png) [@Joe\_Groff](https://forums.swift.org/u/Joe_Groff)\
**Post date:** [July 16, 2018, 6:42pm UTC](https://forums.swift.org/t/unwrap-or-throw-make-the-safe-choice-easier/14453/10 "2018-07-16T18:42:38Z")

</div>

Sure, we could definitely support break/continuing out of general closures. `return` is a little more interesting because there's ambiguity as to which context you'd want to return out of. Autoclosures make the design space a bit simpler because it's (IMO) always obvious which function you'd want to `return` from, since there's no reason to explicitly return out of an autoclosure ~~, and also because autoclosures can only propagate errors via `rethrows`, and `rethrows` functions can currently only propagate errors, not handle or originate errors on their own, which avoids~~ (I was [mistaken about this](https://forums.swift.org/t/unwrap-or-throw-make-the-safe-choice-easier/14453/13)). There's the question of whether the `break`/`continue` pseudo-exception ought to be interceptable by intermediate frames.

---

<div class="post-metadata">

**Author:** ![Jon\_Shier](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jon_shier/32/921_2.png) [@Jon\_Shier](https://forums.swift.org/u/Jon_Shier)\
**Post date:** [July 16, 2018, 7:09pm UTC](https://forums.swift.org/t/unwrap-or-throw-make-the-safe-choice-easier/14453/11 "2018-07-16T19:09:12Z")

</div>

I can't say I'm a fan of adding anything more to `??`, as it would become more than a nil-coalescing operator at that point.

However, I am a huge fan of optional throwing so we can use `try optional.unwrap()`. @ahti's solution is pretty great, and I usually implement a version that doesn't take a custom error.

---

<div class="post-metadata">

**Author:** ![Joe\_Groff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joe_groff/32/1929_2.png) [@Joe\_Groff](https://forums.swift.org/u/Joe_Groff)\
**Post date:** [July 16, 2018, 7:36pm UTC](https://forums.swift.org/t/unwrap-or-throw-make-the-safe-choice-easier/14453/12 "2018-07-16T19:36:35Z")

</div>

> [@Jon\_Shier](#):
>
> I can't say I'm a fan of adding anything more to `??` , as it would become more than a nil-coalescing operator at that point.

This wouldn't be adding anything to `??` specifically, to be clear. You'd be able to use `Never` expressions anywhere as a placeholder where a value isn't available and exiting scope is the only choice.

---

<div class="post-metadata">

**Author:** ![Nevin](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nevin/32/617_2.png) [@Nevin](https://forums.swift.org/u/Nevin)\
**Post date:** [July 16, 2018, 7:39pm UTC](https://forums.swift.org/t/unwrap-or-throw-make-the-safe-choice-easier/14453/13 "2018-07-16T19:39:31Z")

</div>

> [@Joe\_Groff](#):
>
> `rethrows` functions can currently only propagate errors, not handle or originate errors on their own

This is not exactly the case:

```swift
enum CatError: Error { case hairball }
enum DogError: Error { case chasedSkunk }

func foo(_ f: () throws -> Void) rethrows {
    do { try f() }
    catch { throw CatError.hairball }
}

do {
    try foo{ throw DogError.chasedSkunk }
} catch {
    print(error) // hairball
}

```

The above `rethrows` function does not propagate the thrown error, instead creating a new error.

Moreover, a `rethrows` function is also currently [allowed to throw if a local function inside it throws](https://forums.swift.org/t/7339/2)—although this will crash the app if a non-throwing closure was passed to the outer function and the local function does throw.

And also, [`DispatchQueue.sync`](https://github.com/apple/swift/blob/165c5e819ce7a1e24d0befe179bdac24baaf8202/stdlib/public/SDK/Dispatch/Queue.swift#L275) (which `rethrows`) is implemented as simply calling through to a private helper method [`_syncHelper`](https://github.com/apple/swift/blob/165c5e819ce7a1e24d0befe179bdac24baaf8202/stdlib/public/SDK/Dispatch/Queue.swift#L228) which is also `rethrows`, by passing in an unconditionally-throwing closure. The way `_syncHelper` is written it does in fact only throw errors that originated from the closure passed to `sync` but that does not have to be the case.

---

<div class="post-metadata">

**Author:** ![Joe\_Groff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joe_groff/32/1929_2.png) [@Joe\_Groff](https://forums.swift.org/u/Joe_Groff)\
**Post date:** [July 16, 2018, 7:40pm UTC](https://forums.swift.org/t/unwrap-or-throw-make-the-safe-choice-easier/14453/14 "2018-07-16T19:40:46Z")

</div>

Ah, I didn't realize we had implemented that.

---

<div class="post-metadata">

**Author:** ![FranzBusch](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/franzbusch/32/19447_2.png) [@FranzBusch](https://forums.swift.org/u/FranzBusch)\
**Post date:** [July 16, 2018, 7:58pm UTC](https://forums.swift.org/t/unwrap-or-throw-make-the-safe-choice-easier/14453/15 "2018-07-16T19:58:10Z")

</div>

> [@Joe\_Groff](#):
>
> With `Never` in the type system, and `rethrows` autoclosures, we could conceivably make it so that `break` , `continue` , and `return` were also expressions, and plumb them through autoclosures as if they were thrown errors so that they can branch out to the right places in the outer function. This would allow `??` to be used as shorthand for a lot of trivial one-line `guard let` s that unwrap an optional or immediately exit scope on nil.

This sounds amazing to be honest. Having a simply one liner for `guard let x = .. else { return }` would be so useful.

---

<div class="post-metadata">

**Author:** ![hlovatt](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hlovatt/32/3044_2.png) [@hlovatt](https://forums.swift.org/u/hlovatt)\
**Post date:** [July 17, 2018, 12:24am UTC](https://forums.swift.org/t/unwrap-or-throw-make-the-safe-choice-easier/14453/16 "2018-07-17T00:24:29Z")

</div>

I think `break`, `continue`, and `return` as well as `throw` as expressions would be great. I suspect some means of distinguishing `return` from inner closure/function and return from an outer closure/function will be needed. Two possibilities come to mind:

1. Introduce `continue` (with optional value) to mean exit from innermost closure/function and re-define `return` to mean exit from outermost closure/function only. This would be a breaking change (closures currently use `return`) but would match what other languages do, though often called `yield`, and importantly also allow `if`, `for`, `while`, `do`, `try`, and `switch` to become expressions. The `continue` could also be labelled, see next suggestion.

2. Introduce labelled returns for when an exit is not from the innermost closure/function, i.e. `functionName.return` means that the return is from `functionName` and not just the inner closure/function. Similarly labels on `for` etc. and for closures that are declared via a `let` statement can be used for the labelled return. An unlabelled return would behave as it does at present.

I like the power of option 1 and would therefore go with that (a first stage without labelling might be a practical starting point).

---

<div class="post-metadata">

**Author:** ![possen](https://avatars.discourse-cdn.com/v4/letter/p/ed8c4c/32.png) [@possen](https://forums.swift.org/u/possen)\
**Post date:** [July 17, 2018, 4:20am UTC](https://forums.swift.org/t/unwrap-or-throw-make-the-safe-choice-easier/14453/17 "2018-07-17T04:20:27Z")

</div>

I thought I would try to compile in a playground to see what it might look like. I created some names which are expressions, with an \_ after. Not working code, just trying to see what it looks like. I really think this is nice.

> ```
> // could be -> Never when Never-as-bottom is implemented
> func throw_<T>(_ error: Error) throws -> T {
> throw error
> }
> 
> enum Choices {
> case one
> case two
> case three
> }
> 
> var continue_ = 0
> var return_ = 0
> var break_ = 0
> 
> for g in 0..<10 {
> let h: Int? = g
> let p = h ?? continue_
> let q = h ?? break_
> let r = h ?? throw_(Errors.falseResult)
> let s = h ?? return_
> }
> 
> let c = Choices.one
> 
> func call() throws {
> let val: Int? = nil
> let a = val ?? return_
> let b = val ?? throw_(Errors.falseResult)
> 
> switch c {
> case .one:
> let c = val ?? break_
> case .two:
> let d = val ?? return_
> case .three where val ?? break_: // as expected, will not compile
> print()
> }
> }
> 
> // also, think this might work.
> func call2() -> Int {
> let val: Int? = nil
> let a = val ?? return_(10)
> }
> 
> ```

---

<div class="post-metadata">

**Author:** ![possen](https://avatars.discourse-cdn.com/v4/letter/p/ed8c4c/32.png) [@possen](https://forums.swift.org/u/possen)\
**Post date:** [July 17, 2018, 4:45am UTC](https://forums.swift.org/t/unwrap-or-throw-make-the-safe-choice-easier/14453/18 "2018-07-17T04:45:04Z")

</div>

I agree making other control flow statements into expressions would also be nice.

---

<div class="post-metadata">

**Author:** ![Karl](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Karl](https://forums.swift.org/u/Karl)\
**Post date:** [July 20, 2018, 10:16am UTC](https://forums.swift.org/t/unwrap-or-throw-make-the-safe-choice-easier/14453/19 "2018-07-20T10:16:01Z")

</div>

Control flow as expressions would be great for unit testing. Test code usually just cares about the one, small thing it is testing, and anything unexpected should fail the entire test. At the moment, functions like `XCTAssertNoThrow` have no way to bail out of the enclosing test function if they fail.

For example:

```swift
fun returnsOptionalVal() -> MyVal? { ... }

// Currently:
guard let myVal = returnsOptionalVal() else {
  XCTFail("unexpected nil")
  return
}

// Could be:
let myVal = XCTAssertNotNil(returnsOptionalVal()) // else: fail, return Void.

```

It can make writing tests a pain, because although specialised functions like `XCTAssertNoThrow` provide MUCH better diagnostics, they just totally impractical to use without the ability to return from the function. For example, the only way I found to use `XCTAssertNoThrow` with a throwing initialiser is via a temporary optional:

```swift
struct MyVal { init() throws { ... } }

var _myVal: MyVal?
XCTAssertNoThrow(_myVal = try MyVal())
let myVal = _myVal!

// I want to write:
let myVal = XCTAssertNoThrow(try MyVal()) // else: fail, return Void.

```

---

<div class="post-metadata">

**Author:** ![benrimmington](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/benrimmington/32/695_2.png) [@benrimmington](https://forums.swift.org/u/benrimmington)\
**Post date:** [July 20, 2018, 11:18pm UTC](https://forums.swift.org/t/unwrap-or-throw-make-the-safe-choice-easier/14453/20 "2018-07-20T23:18:54Z")

</div>

@Karl, you can add the `throws` keyword to your test method, and it will fail if an error is caught by the `XCTest` framework (at least on macOS).

```swift
class MyValTests: XCTestCase {
  func testMyVal() throws {
    let myVal = try MyVal()
    /* ... */
  }
}

```

[Next page](https://forums.swift.org/t/unwrap-or-throw-make-the-safe-choice-easier/14453.md?page=2)
