Avatar
omochimetaru 11/7/2018 7:12 AM
@hiragram public protocol DecodableByJSON : Decodable { associatedtype JSON : Decodable init(from json: JSON) throws } extension DecodableByJSON { public init(from decoder: Decoder) throws { let json = try JSON(from: decoder) try self.init(from: json) } } public struct GenericVector3<T> { public var x: T public var y: T public var z: T public init(_ x: T, _ y: T, _ z: T) { self.x = x self.y = y self.z = z } } public typealias GenericVector3JSON<T> = Array<T> extension GenericVector3 : DecodableByJSON /*(これが必要) , Decodable*/ where T : Decodable { public typealias JSON = GenericVector3JSON<T> public init(from json: GenericVector3JSON<T>) throws { guard json.count == 3 else { fatalError() } self.init(json[0], json[1], json[2]) } }
7:13 AM
ConditionalConformanceでは間接的にProtocolを適用できないんですね (ここでは「DecodableByJSONを満たさせると自動的にDecodableを満たす」ことを指して「間接的」と呼んだ