# Convenience member on Result when when Success is Void

**URL:** <https://forums.swift.org/t/convenience-member-on-result-when-when-success-is-void/36134>\
**Category:** Pitches\
**Created:** [May 4, 2020, 10:10am UTC](https://forums.swift.org/t/convenience-member-on-result-when-when-success-is-void/36134 "2020-05-04T10:10:23Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![tcldr](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tcldr/32/24405_2.png) [@tcldr](https://forums.swift.org/u/tcldr)\
**Post date:** [May 4, 2020, 10:10am UTC](https://forums.swift.org/t/convenience-member-on-result-when-when-success-is-void/36134/1 "2020-05-04T10:10:23Z")

</div>

This is a tiny refinement to the std lib that to provide a convenience member on Result when when Success is Void.

Result types with a success type of Void are commonly used when no further information about the result of the operation are required – other than to know it was successful. In these cases, it is necessary to return a Result via `Result.success(())`.

The double parentheses can sometimes become an impediment to legibility – especially if wrapped within another set of parentheses, e.g. as an argument within a function call.

```swift
 someHandler(.success(()))

```

I propose a tiny addition to the std-lib to iron out the kink:

```swift
extension Result where Success == Void {
    public static var success: Result { .success(()) }
}

```

Which smoothes out the above example to:

```swift
 someHandler(.success)

```

Whilst I can't think of elsewhere in the Swift std-lib where this kind of specialisation of types with Void type arguments exists, there is prior history elsewhere in the Apple eco-system. Specifically [Combine's PassthroughSubject.send()](https://developer.apple.com/documentation/combine/passthroughsubject/3326007-send) which presumably added the syntactic sugar to clean-up the SwiftUI api.

---

<div class="post-metadata">

**Author:** ![DevAndArtist](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/devandartist/32/287_2.png) [@DevAndArtist](https://forums.swift.org/u/DevAndArtist)\
**Post date:** [May 4, 2020, 10:57am UTC](https://forums.swift.org/t/convenience-member-on-result-when-when-success-is-void/36134/2 "2020-05-04T10:57:16Z")

</div>

I understand the pitch, but isn‘t the usage of `SomeError?` preferred over `Result<Void, SomeError>`?

---

<div class="post-metadata">

**Author:** ![tcldr](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tcldr/32/24405_2.png) [@tcldr](https://forums.swift.org/u/tcldr)\
**Post date:** [May 4, 2020, 11:00am UTC](https://forums.swift.org/t/convenience-member-on-result-when-when-success-is-void/36134/3 "2020-05-04T11:00:36Z")

</div>

Often it makes sense for your API to consistently return Results over chopping and changing between Optionals and Results. That's because Results are composable with flatMap, etc.

---

<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:** [May 4, 2020, 11:10am UTC](https://forums.swift.org/t/convenience-member-on-result-when-when-success-is-void/36134/4 "2020-05-04T11:10:05Z")

</div>

> [@tcldr](#):
>
> The double parentheses can sometimes become an impediment to legibility – especially if wrapped within another set of parentheses, e.g. as an argument within a function call.
> 
> ```swift
> someHandler(.success(()))
> 
> ```
> 
> I propose a tiny addition to the std-lib to iron out the kink:

In lieu of an addition to the standard library, why not use the spelling that's preferred in Swift: `.success(Void)`? This is not the only circumstance in which `()`-as-`Void` is less than readable, and it's precisely for that reason that `Void` is preferred.

---

<div class="post-metadata">

**Author:** ![tcldr](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tcldr/32/24405_2.png) [@tcldr](https://forums.swift.org/u/tcldr)\
**Post date:** [May 4, 2020, 11:13am UTC](https://forums.swift.org/t/convenience-member-on-result-when-when-success-is-void/36134/5 "2020-05-04T11:13:10Z")

</div>

Hmmm, I thought it was the other way round: `Void` should be used as the return type and '()' as the argument type?

In any case, I get the following error with that approach: 'Cannot convert value of type 'Void.Type' to expected argument type '()''

---

<div class="post-metadata">

**Author:** ![DevAndArtist](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/devandartist/32/287_2.png) [@DevAndArtist](https://forums.swift.org/u/DevAndArtist)\
**Post date:** [May 4, 2020, 11:13am UTC](https://forums.swift.org/t/convenience-member-on-result-when-when-success-is-void/36134/6 "2020-05-04T11:13:11Z")

</div>

> [@xwu](#):
>
> In lieu of an addition to the standard library, why not use the spelling that's preferred in Swift: `.success(Void)` ?

That won't even compile here as `Void` is a type, while `()` is a value of type `Void` (or the `typealias Void = ()`, which you refer as less readable).

---

<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:** [May 4, 2020, 11:16am UTC](https://forums.swift.org/t/convenience-member-on-result-when-when-success-is-void/36134/7 "2020-05-04T11:16:08Z")

</div>

> [@DevAndArtist](#):
>
> That won't even compile here as `Void` is a type, while `()` is a value of type `Void` (or the `typealias Void = ()` , which you refer as less readable).

Whoops, brainfart. 🙂

---

<div class="post-metadata">

**Author:** ![zzt4](https://avatars.discourse-cdn.com/v4/letter/z/e495f1/32.png) [@zzt4](https://forums.swift.org/u/zzt4)\
**Post date:** [May 7, 2020, 11:31pm UTC](https://forums.swift.org/t/convenience-member-on-result-when-when-success-is-void/36134/8 "2020-05-07T23:31:26Z")

</div>

+1 I have been reaching for `.success(())` personally when facing this. One of my coworkers prefers the more verbose `.success(Void())`. I would prefer to just use this extension instead. I might even consider adding this extension in the meantime.

---

<div class="post-metadata">

**Author:** ![John\_McCall](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/john_mccall/32/1351_2.png) [@John\_McCall](https://forums.swift.org/u/John_McCall)\
**Post date:** [May 8, 2020, 12:53am UTC](https://forums.swift.org/t/convenience-member-on-result-when-when-success-is-void/36134/9 "2020-05-08T00:53:19Z")

</div>

This extension would make `.success` ambiguous absent any type context (such as a call).

---

<div class="post-metadata">

**Author:** ![tcldr](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tcldr/32/24405_2.png) [@tcldr](https://forums.swift.org/u/tcldr)\
**Post date:** [May 8, 2020, 6:15am UTC](https://forums.swift.org/t/convenience-member-on-result-when-when-success-is-void/36134/10 "2020-05-08T06:15:03Z")

</div>

Yes, that’s true – I'd missed that.

So instead:

```swift
extension Result where Success == Void {
    public static func success() -> Self { .success(()) }
}

```

Which hopefully is still a big improvement on ergonomics/legibility.

```swift
 someHandler(.success())

```

Side note: maybe there is eventually room for a more wide-ranging, complex proposal: any function with Void parameters would have a counterpart function generated where the default `()`s are specified/provided.

e.g. with Combine's `PassthroughSubject.send(_:)`, this would automatically derive `PassthroughSubject.send()` when PassthroughSubject's Output type is Void. For enums, it would automatically generate the appropriate counterpart static var (as described above for Result.) Of course, if a user-defined equivalent already exists – that would take precedence.

---

<div class="post-metadata">

**Author:** ![jayton](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jayton/32/5592_2.png) [@jayton](https://forums.swift.org/u/jayton)\
**Post date:** [May 8, 2020, 9:22am UTC](https://forums.swift.org/t/convenience-member-on-result-when-when-success-is-void/36134/11 "2020-05-08T09:22:15Z")

</div>

This is a pretty common issue with generics and PATs, and can be hard to work around; I’d really like to see a language-level solution if one is feasible.

One way to think of it would be that for value parameters of a generic/associated type that resolve to `Void`, there should be a synthesized default value of `()`.

---

<div class="post-metadata">

**Author:** ![hfhbd](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hfhbd/32/7340_2.png) [@hfhbd](https://forums.swift.org/u/hfhbd)\
**Post date:** [May 11, 2020, 4:25pm UTC](https://forums.swift.org/t/convenience-member-on-result-when-when-success-is-void/36134/12 "2020-05-11T16:25:34Z")

</div>

There is already this [pull request](https://github.com/apple/swift/pull/26471) but missing a proposal.

I started writing a [proposal](https://github.com/hfhbd/swift-evolution/blob/master/proposals/Results-Improvements.md), including more improvements to the `Result` type.

@tcldr feel free to contribute 🙂

---

<div class="post-metadata">

**Author:** ![ronyfadel](https://avatars.discourse-cdn.com/v4/letter/r/fbc32d/32.png) [@ronyfadel](https://forums.swift.org/u/ronyfadel)\
**Post date:** [June 3, 2020, 12:26am UTC](https://forums.swift.org/t/convenience-member-on-result-when-when-success-is-void/36134/13 "2020-06-03T00:26:07Z")

</div>

What's the issue with having `.success` ambiguous without any type context?  
i.e. can you provide an example that twists the programmer's arm while coding, in order to favor `.success()` over `.success`?

Thanks John!

---

<div class="post-metadata">

**Author:** ![David\_Catmull](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/david_catmull/32/1007_2.png) [@David\_Catmull](https://forums.swift.org/u/David_Catmull)\
**Post date:** [June 6, 2021, 7:38pm UTC](https://forums.swift.org/t/convenience-member-on-result-when-when-success-is-void/36134/14 "2021-06-06T19:38:05Z")

</div>

I just found this thread a year later because I ran into this issue myself. Did anything ever come of this idea? I'm also curious about the answer to @ronyfadel `s question.

---

<div class="post-metadata">

**Author:** ![Dmitriy\_Ignatyev](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dmitriy_ignatyev/32/11340_2.png) [@Dmitriy\_Ignatyev](https://forums.swift.org/u/Dmitriy_Ignatyev)\
**Post date:** [June 11, 2021, 1:00am UTC](https://forums.swift.org/t/convenience-member-on-result-when-when-success-is-void/36134/15 "2021-06-11T01:00:31Z")

</div>

Thanks for this pitch. We are already using such extension in our project, and will be glad to see it in Swift.

```swift
  public func validate(_ text: String) -> Result<Void, Error> {
    switch luhnAlgorithm(value: text) {
    case true: return .success
    case false: return .failure(error)
    }
  }

```

`return .success` is much more readable than `return .success(Void())` or `return .success(())`

But as @John_McCall wrote, sometimes it causes an compiler error:

```swift
static func validate<ErrorType: Error>(text: String,
                                       regEx: String,
                                       error: ErrorType) -> Result<Void, ErrorType> {
    let predicate = NSPredicate(format: "SELF MATCHES %@", regEx)
    let isValid = predicate.evaluate(with: text)

    switch isValid {
    case true: return .success // Compiler error: Member 'success' expects argument of type 'Void'
    case false: return .failure(error)
    }
  }

public func allSuccessOrFirstFailure<Failure: Error>(_ results: [Result<Void, Failure>]) -> Result<Void, Failure> {
  results.first { $0.isFailure } ?? .success // Compiler error: Member 'success' expects argument of type 'Void'
}

func processResult(_ result: Result<Void, DecodingError>) { ... }
processResult(.success) // Compiler error: Member 'success' expects argument of type 'Void'

```

This error reason is clear, but from the point of view of human perception `return .success` looks better.

---

<div class="post-metadata">

**Author:** ![byohay](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/byohay/32/12527_2.png) [@byohay](https://forums.swift.org/u/byohay)\
**Post date:** [January 1, 2023, 5:20pm UTC](https://forums.swift.org/t/convenience-member-on-result-when-when-success-is-void/36134/16 "2023-01-01T17:20:47Z")

</div>

I don't know if it's because the compiler got more advanced, but all the examples you posted don't emit a compilation error, at least not on Xcode 14.2.  
Is there any other ambiguity caused by declaring `.success` without parenthesis?

---

<div class="post-metadata">

**Author:** ![santicarmo31](https://avatars.discourse-cdn.com/v4/letter/s/96bed5/32.png) [@santicarmo31](https://forums.swift.org/u/santicarmo31)\
**Post date:** [August 29, 2024, 8:25pm UTC](https://forums.swift.org/t/convenience-member-on-result-when-when-success-is-void/36134/17 "2024-08-29T20:25:24Z")

</div>

Any news on this idea?
