# \[Pitch\] Optional.orThrow

**URL:** <https://forums.swift.org/t/pitch-optional-orthrow/69995>\
**Category:** Pitches\
**Created:** [February 12, 2024, 9:34pm UTC](https://forums.swift.org/t/pitch-optional-orthrow/69995 "2024-02-12T21:34:35Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [February 12, 2024, 10:19pm UTC](https://forums.swift.org/t/pitch-optional-orthrow/69995/5 "2024-02-12T22:19:56Z")

</div>

See also previous threads on this topic:

> [@Throw on nil](https://forums.swift.org/t/throw-on-nil/39970):
>
> I find myself wanting to define an operation that converts failable initializers to throwing ones. When I have a throwing initializer contains multiple failing initializers, I think it improves the clarity and readability of the code. (see below) UPDATE: Leaving the original implementation but I am not opposed to changing the spelling or making this an operator or whatever. Lots of great ideas and info below. extension Optional { func orThrow(\_ error: @autoclosure () -\> Error) throws -\> W…

> [@Combining ? with throwing when there's no reasonable default](https://forums.swift.org/t/combining-with-throwing-when-theres-no-reasonable-default/20722):
>
> If we want to say, "use this or if it's nil, that default", we can write: let a = maybeA ?? A() However, saying "use this or if it's nil, throw" is more verbose (if descriptive): guard let a = maybeA else { throw SomeError() } One might imagine writing let a = maybeA ?? throw SomeError() but this doesn't work: throw X is a statement, not an expression, so it doesn't parse. As @Nevin [notes](https://forums.swift.org/t/elvis-operator/11368/12), we can make the right-hand side an explicit closure evaluation: let a = try maybeA() ?? { throw Som…

In particular, re @ellie20's comment, see:

> [@Combining ? with throwing when there's no reasonable default](https://forums.swift.org/t/combining-with-throwing-when-theres-no-reasonable-default/20722/23):
>
> I agree. Several other things were different back in Swift 1 days while we were discussing this - e.g. we didn't have Never at the time. I'm +1 on moving throw to being an expression.

---

_[View the full topic](https://forums.swift.org/t/pitch-optional-orthrow/69995)._
