Skip to content

Instantly share code, notes, and snippets.

@r-peck
Last active May 23, 2017 20:21
Show Gist options
  • Save r-peck/0bbd41baef26c9b87b4ef8469aafa281 to your computer and use it in GitHub Desktop.
Save r-peck/0bbd41baef26c9b87b4ef8469aafa281 to your computer and use it in GitHub Desktop.
struct Hole<T> {
static var value: T {
fatalError("Unfilled hole of type `\(String(describing: T.self))`")
}
}
enum Result<T, E: Error> {
case success(T)
case failure(E)
func map<U>(_ tranform: (T) -> U) -> Result<U, E> {
switch self {
case let .success(value):
return .success(tranform(value))
case let .failure(error):
return .failure(error)
}
}
}
func getResult() -> Result<String, NSError> {
return Result
.success(3)
.map { value in
Hole.value // quick help shows you need to return String
}
}
getResult() // fatal error: Unfilled hole of type `String`
// Could also use a free function, but quick help isn't quite as nice since it shows a function signature instead of the exact type
func hole<T>() -> T {
fatalError("Unfilled hole of type `\(String(describing: T.self))`")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment