Skip to content

Instantly share code, notes, and snippets.

@glessard
Created November 14, 2017 23:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glessard/48f4c1305ac20b1b99c1bbdc2fb6290c to your computer and use it in GitHub Desktop.
Save glessard/48f4c1305ac20b1b99c1bbdc2fb6290c to your computer and use it in GitHub Desktop.
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