Skip to content

Commit 3f74d22

Browse files
committed
Update style, add alternate spellings.
1 parent 325f383 commit 3f74d22

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

proposals/NNNN-add-result.md

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,12 +256,14 @@ extension Result where Error == Swift.Error {
256256
/// the previous error value.
257257
public func flatMap<NewValue>(
258258
_ transform: (Value) throws -> NewValue
259-
) -> Result<NewValue, Swift.Error>
259+
) -> Result<NewValue, Error>
260260
}
261261

262-
extension Result: Equatable where Value: Equatable, Error: Equatable { }
262+
extension Result : Equatable where Value : Equatable, Error : Equatable { }
263+
264+
extension Result : Hashable where Value : Hashable, Error : Hashable { }
263265

264-
extension Result: Hashable where Value: Hashable, Error: Hashable { }
266+
extension Result : CustomDebugStringConvertible { }
265267
```
266268

267269
## Other Languages
@@ -286,6 +288,28 @@ This proposal adds a type to the standard library and so will affect the ABI onc
286288
Addition of `Result<Value, Error>` should be future proof against future needs surrounding error handling.
287289

288290
## Alternatives considered
291+
292+
### Alternative Spellings of `Result<Value, Error>`
293+
Two alternate spellings were proposed:
294+
295+
```swift
296+
enum Result<Value, Error> {
297+
case value(Value)
298+
case error(Error)
299+
}
300+
```
301+
and
302+
303+
```swift
304+
enum Result<Wrapped, Failure> {
305+
case value(Value)
306+
case error(Error)
307+
}
308+
```
309+
However, these spellings emphasize more of a similarity to `Optional` than seems appropriate. Emphasizing `Result` good/bad, yes/no, success/failure nature seems more inline with the typical usage and meaning of the type. Using `success` and `failure` cases makes that usage completely clear. The `Value`/`Error` generic types appropriately convey the usage of the individual types along the same lines. Ultimately, however, the proposed spelling benefits from the fact that's it's the most common spelling implemented by the Swift community, making it the easiest to drop in and replace existing implementations, as well as benefitting from the current level of community knowledge around the type.
310+
311+
### Alternatives to `Result<Value, Error>`
312+
289313
- `Result<T>`: A `Result` without a generic error type fits well with the current error design in Swift. However, it prevents the future addition of typed error handling (typed `throws`), as well as locking all `Result` usage into failure types which conform to `Error`.
290314

291315
- `Result<T, E: Error>`: A `Result` that constrains its error type to just those types conforming to `Error` allows for fully typed error handling. However, this type is incompatible with current Swift error handling, as it cannot capture unconstrained `Error` values. This would require either casting to a specific error type (commonly `NSError`, which is an anti-pattern) or the addition of a `Error` box type, such as `AnyError`. Additionally, the constraint prevents future growth towards capturing non-`Error` conforming types.

0 commit comments

Comments
 (0)