Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. struct Hole<T> {
  2. static var value: T {
  3. fatalError("Unfilled hole of type `\(String(describing: T.self))`")
  4. }
  5. }
  6.  
  7. enum Result<T, E: Error> {
  8. case success(T)
  9. case failure(E)
  10.  
  11. func map<U>(_ tranform: (T) -> U) -> Result<U, E> {
  12. switch self {
  13. case let .success(value):
  14. return .success(tranform(value))
  15.  
  16. case let .failure(error):
  17. return .failure(error)
  18. }
  19. }
  20. }
  21.  
  22. func getResult() -> Result<String, NSError> {
  23. return Result
  24. .success(3)
  25. .map { value in
  26. Hole.value // quick help shows you need to return String
  27. }
  28. }
  29.  
  30. getResult() // fatal error: Unfilled hole of type `String`
  31.  
  32. // 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
  33. func hole<T>() -> T {
  34. fatalError("Unfilled hole of type `\(String(describing: T.self))`")
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement