Skip to content

Instantly share code, notes, and snippets.

@jonhull
jonhull / Result.Swift
Created October 26, 2018 19:37
This is a Result type which I would like to see picked up by the Swift Standard Library. It allows custom error types, but also has easy interop with native Swift Error Handling
import Foundation
///A Result type which indicates the success or failure of a task. Has a generic Value, and uses a Swift.Error for the failure case. Specialization of SpecificResult.
public typealias Result<T> = SpecificResult<T,Swift.Error>
///A Result type which indicates the success or failure of a task using an enum to hold the resulting (generic) value in the case of success or (generic) error in the case of failure.
public enum SpecificResult<Value,Error> {
///The task succeeded and returned this value
case success(Value)
///The task failed with this error
public final class Source<T>:SourceProtocol, ExpressibleByArrayLiteral,Collection {
public typealias Element = T
private let box:_sourceBoxBase<T>
public init<P:SourceProtocol>(_ base:P) where P.Element == T {
self.box = _sourceBox(base)
}
public init(_ constant:T) {
self.box = _sourceBox(ConstantSource(constant))