Last active
August 28, 2017 19:08
-
-
Save vhart/ef2be2ffbdf4f462f6a98b515aeff31b to your computer and use it in GitHub Desktop.
Type Erasure Protocol
This file contains 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
enum Result<T, E: Error> { | |
case success(T) | |
case failure(E) | |
} | |
enum NetworkError: Error { | |
case invalidJson | |
case badRequest | |
case timeout | |
} | |
protocol JsonDeserializer { | |
associatedtype Response | |
func parse(json: [String: Any]) -> Response | |
} | |
protocol NetworkSession { | |
associatedtype Payload | |
func get(url: URL, parameters: [String: String], onComplete: (Result<Payload, NetworkError>) -> Void) | |
} | |
class Session<P>: NetworkSession { | |
typealias Payload = P | |
let deserializer: JsonDeserializer | |
init(deserializer: JsonDeserializer) { | |
self.deserializer = deserializer | |
} | |
func get(url: URL, parameters: [String: String], onComplete: (Result<P, NetworkError>) -> Void) {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment