This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public struct Result<Value> | |
{ | |
private let state: State<Value> | |
init(_ value: Value) { | |
state = .value(value) | |
} | |
init(_ error: Error) { | |
state = .error(error) | |
} | |
init(_ f: () throws -> Value) { | |
do { | |
let value = try f() | |
state = .value(value) | |
} | |
catch { | |
state = .error(error) | |
} | |
} | |
public func get() throws -> Value { | |
switch state { | |
case .value(let value): return value | |
case .error(let error): throw error | |
} | |
} | |
} | |
private enum State<Value> | |
{ | |
case value(Value) | |
case error(Error) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment