Skip to content

Instantly share code, notes, and snippets.

@vhart
Last active August 28, 2017 19:08
Show Gist options
  • Save vhart/ef2be2ffbdf4f462f6a98b515aeff31b to your computer and use it in GitHub Desktop.
Save vhart/ef2be2ffbdf4f462f6a98b515aeff31b to your computer and use it in GitHub Desktop.
Type Erasure Protocol
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