Last active
May 23, 2017 20:21
-
-
Save r-peck/0bbd41baef26c9b87b4ef8469aafa281 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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