Last active
October 6, 2018 08:12
-
-
Save TerryCK/9ac487cc8909d32e3d21d7279c658429 to your computer and use it in GitHub Desktop.
Generic class inheritance
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
import Foundation | |
protocol Network { | |
typealias SuccessHandler = (Array<Response>) -> Void | |
associatedtype Response: Codable | |
var payloads: Array<Response> { get } | |
func fire(onSuccess: SuccessHandler) | |
} | |
extension NetworkParent where Response == Int { | |
func fire(onSuccess: SuccessHandler) { | |
// Special Response type case | |
} | |
} | |
class NetworkParent<Response: Codable> { | |
var payloads: Array<Response> = [] | |
init(type: Response.Type) { } | |
func fire(onSuccess: SuccessHandler) { | |
// General case | |
} | |
} | |
extension NetworkChild where Response == String { | |
func fire(onSuccess: SuccessHandler) { | |
// Special Response type case on Child class | |
} | |
} | |
final class NetworkChild<Response: Codable>: NetworkParent<Response> { } | |
struct Payload: Codable { let head: String, body: String } | |
let networkChild = NetworkChild(type: Payload.self) | |
print(type(of: networkChild.payloads)) | |
import Foundation | |
protocol Network { | |
typealias SuccessHandler = (Array<Response>) -> Void | |
associatedtype Response: Codable, PayloadProtocol | |
var payloads: Array<Response> { get } | |
func fire(onSuccess: SuccessHandler) | |
} | |
extension NetworkParent where Response.Body == Int { | |
func fire(onSuccess: SuccessHandler) { | |
// Special Response type case | |
} | |
} | |
class NetworkParent<Response: Codable & PayloadProtocol>: Network { | |
var payloads: Array<Response> = [] | |
init(type: Response.Type) { } | |
func fire(onSuccess: SuccessHandler) { | |
// General case | |
} | |
} | |
extension NetworkChild where Response.Body == String { | |
// func fire(onSuccess: SuccessHandler) { | |
// // Special Response type case on Child class | |
// } | |
} | |
final class NetworkChild<Response: Codable & PayloadProtocol>: NetworkParent<Response> { } | |
protocol PayloadProtocol { | |
associatedtype Body: Codable | |
var body: Body { get } | |
} | |
struct Payload<Body: Codable>: Codable, PayloadProtocol { | |
let head: String, body: Body } | |
let networkChild = NetworkChild(type: Payload<Int>.self) | |
print(type(of: networkChild.payloads)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment