Reputation: 360
The issue is when a struct conforms to a protocol (let's call it PA) and Decodable, but PA imposes a property with a type that is not Decodable. Example:
protocol PA {
var b: [PB]? { get }
}
protocol PB {}
struct SA: PA, Decodable {
let b: [PB]? // SA's conformance to Decodable wants this to be [Decodable], but PA's conformance imposes [PB]
}
struct SB: PB, Decodable {}
the code above refuses to compile, with:
Changing that line to:
let b: [PB & Decodable]?
does not work either and gives:
Note that the 4th line is non-sense: "'[Decodable & PB]?' does not conform to 'Decodable'". Wait what?
Any suggestion?
Upvotes: 0
Views: 397
Reputation: 54486
You may create a mixed protocol:
protocol PADecodable {
var b: [PB & Decodable]? { get }
}
struct SA: PADecodable {
let b: [PB & Decodable]?
}
Upvotes: 1
Reputation: 568
you can fix it by:
protocol PA {
var b: [PB]? { get }
}
protocol PB {}
struct SA<T: PB & Codable>: PA, Codable {
private var _b: [T]?
var b: [PB]? {
return _b
}
}
Upvotes: 0