# \[Pitch\] Result success and failure accessors

**URL:** <https://forums.swift.org/t/pitch-result-success-and-failure-accessors/82396>\
**Category:** Pitches\
**Tags:** result\
**Created:** [September 27, 2025, 3:10am UTC](https://forums.swift.org/t/pitch-result-success-and-failure-accessors/82396 "2025-09-27T03:10:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![corujautx](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/corujautx/32/42979_2.png) [@corujautx](https://forums.swift.org/u/corujautx)\
**Post date:** [September 27, 2025, 3:10am UTC](https://forums.swift.org/t/pitch-result-success-and-failure-accessors/82396/1 "2025-09-27T03:10:28Z")

</div>

Hi folks!

This is a bit inspired by Rust's [unwrap\_or](https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap_or) / [unwrap\_or\_else](https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap_or) methods.

The idea is having `success(or:)` / `failure(or:)`methods in `Swift.Result` to allow to lift a success or failure without running into throwing errors like exists today in `get()` or having to do a pattern match.

```swift
func returnsResult() -> Result<Int, any Error> {}

// current matching
func returnsDefaultValue() -> Int {
  guard case let .success(success) = returnsResult() else { return 1 }
  return success
}

// or even with a try?
func returnsDefaultValue() -> Int {
  try? returnsValue().get() ?? 1
}

// would become instead
func returnsDefaultValue() {
  returnsResult().success(or: 1)
}

```

The obvious advantage is in how you read this when you have multiple calls that return `Result`

```swift
async let request1 = service.request1().get()
async let request2 = service.request2().get()

let (response1, response2) = (
  (try? await request1) ?? 1,
  (try? await request2) ?? 2
)

// would become instead

async let request1 = service.request1().success(or: 1)
async let request2 = service.request2().success(or: 2)

let (response1, response2) = await (request1, request2)

```

Below is a quick draft in how I'm thinking:

```swift
extension Result {
  /// Returns the success value contained in the `Result` enum or the value generated by the closure in case of failure
  /// - Parameter otherwise: A closure that generates a value to be returned in case `Result` is a failure
  /// - Returns:The success value present in `Result` or the value generated by the closure in case `Result` is a failure
  @_alwaysEmitIntoClient
  func success(or otherwise: () -> Success) -> Success {
    guard case let .success(success) = self else { return otherwise() }
    return success
  }

  /// Returns the success value contained in the `Result` enum or the value passed to the function
  /// - Parameter otherwise: The value to be returned in case `Result` is a failure
  /// - Returns:The success value present in `Result` or the value passed to the function in case `Result` is a failure
  @_alwaysEmitIntoClient
  func success(or otherwise: Success) -> Success {
    success(or: { otherwise })
  }
}

extension Result {
  /// Returns the failure value contained in the `Result` enum or the value generated by the closure in case of success
  /// - Parameter otherwise: A closure that generates a value to be returned in case `Result` is a success
  /// - Returns:The failure value present in `Result` or the value generated by the closure in case `Result` is a success
  @_alwaysEmitIntoClient
  func failure(or otherwise: () -> Failure) -> Failure {
    guard case let .failure(failure) = self else { return otherwise() }
    return failure
  }

  /// Returns the failure value contained in the `Result` enum or the value passed to the function
  /// - Parameter otherwise: The value to be returned in case `Result` is a success
  /// - Returns:The failure value present in `Result` or the value passed to the function in case `Result` is a success
  @_alwaysEmitIntoClient
  func failure(or otherwise: Failure) -> Failure {
    failure(or: { otherwise })
  }
}

extension Result {
  /// Immediately returns the success value contained in the `Result` enum or awaits the value generated by the closure in case of failure
  /// - Parameter otherwise: A closure that generates a value to be returned in case `Result` is a failure
  /// - Returns:The success value present in `Result` or the value generated by the closure in case `Result` is a failure
  @_alwaysEmitIntoClient
  func success(or otherwise: () async -> Success) async -> Success {
    guard case let .success(success) = self else { return await otherwise() }
    return success
  }

  /// Immediately returns the failure value contained in the `Result` enum or awaits the value generated by the closure in case of success
  /// - Parameter otherwise: A closure that generates a value to be returned in case `Result` is a success
  /// - Returns:The failure value present in `Result` or the value generated by the closure in case `Result` is a success
  @_alwaysEmitIntoClient
  func failure(or otherwise: () async -> Failure) async -> Failure {
    guard case let .failure(failure) = self else { return await otherwise() }
    return failure
  }
}

```

---

<div class="post-metadata">

**Author:** ![Danny](https://avatars.discourse-cdn.com/v4/letter/d/848f3c/32.png) [@Danny](https://forums.swift.org/u/Danny)\
**Post date:** [September 27, 2025, 4:18am UTC](https://forums.swift.org/t/pitch-result-success-and-failure-accessors/82396/2 "2025-09-27T04:18:34Z")

</div>

(post deleted by author)

---

<div class="post-metadata">

**Author:** ![corujautx](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/corujautx/32/42979_2.png) [@corujautx](https://forums.swift.org/u/corujautx)\
**Post date:** [September 27, 2025, 5:33am UTC](https://forums.swift.org/t/pitch-result-success-and-failure-accessors/82396/3 "2025-09-27T05:33:09Z")

</div>

Fair point, I actually haven't thought about writing like this.

I think it's just a bit more expressive than lifting it into an optional and then using the `??` operator.

Perhaps this can evolve into `Result` also supporting the `??` operator or having some sort of syntactic sugar to lift typed throw functions into `Result` types?

The main motivation is also for chains of transformation.  
Take in consideration something like this:

```swift
// given func request() async -> Result<MyType, any Error>

await request()
  .map(MyOtherType.self)
  .mapError(MyError.self)
  .success(or: MyOtherType())

// versus current

try? await request()
  .map(MyOtherType.self)
  .mapError(MyError.self)
  .get() ?? MyOtherType() 

```

I feel it's a bit confusing to have the `try?` there because the addition of `.get()`. Ofc if the community feels this is not needed, then happy to pull the idea out.

There's also the possibility in that design of having something similar to Rust's `unwrap_or_else` as in you pass a closure to the `success(or:)` function

---

<div class="post-metadata">

**Author:** ![Nobody1707](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nobody1707/32/2169_2.png) [@Nobody1707](https://forums.swift.org/u/Nobody1707)\
**Post date:** [September 27, 2025, 11:09pm UTC](https://forums.swift.org/t/pitch-result-success-and-failure-accessors/82396/5 "2025-09-27T23:09:47Z")

</div>

Also, even if we were going to add something like this, I think your `otherwise` parameters should probably be auto-closures.

---

<div class="post-metadata">

**Author:** ![corujautx](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/corujautx/32/42979_2.png) [@corujautx](https://forums.swift.org/u/corujautx)\
**Post date:** [October 6, 2025, 9:52am UTC](https://forums.swift.org/t/pitch-result-success-and-failure-accessors/82396/7 "2025-10-06T09:52:26Z")

</div>

I like this, but playing devil's advocate here, I feel it might be semantically confusing to have the operator for errors in that case. `.success(or:)` feels semantically more clear about what's happening.
