Skip to content

Instantly share code, notes, and snippets.

@hborders
Created February 20, 2015 15:09
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 hborders/61c24cc77ff3c5d7ba34 to your computer and use it in GitHub Desktop.
Save hborders/61c24cc77ff3c5d7ba34 to your computer and use it in GitHub Desktop.
Is there a better way to handle the failure case?
class Box<A> {
public let value: A
init(_ value: A) { self.value = value }
}
enum Result<A> {
case Success(Box<A>)
case Failure(NSError)
}
class Fizz {
}
class Buzz {
}
class Foo {
func foo() -> Result<Fizz> {
let result = // a result
return result
}
}
class Bar {
func bar(foo: Foo) -> Result<Buzz> {
switch foo.foo() {
case .Success(let fizzBox):
let buzz = // a buzz
return .Success(Box(buzz))
// There must be a swiftier way to handle the failure case!
case .Failure(let error):
return .Failure(error)
}
}
}
@hborders
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment