We just switched to swift 4.1 and we are having some difficulties with type conformance for Arrays. Here is the old way:
public typealias XDRCodable = XDREncodable & XDRDecodable
public protocol XDREncodable: Encodable {
func xdrEncode(to encoder: XDREncoder) throws
}
public protocol XDRDecodable: Decodable {
init(fromBinary decoder: XDRDecoder) throws
init(fromBinary decoder: XDRDecoder, count: Int) throws
}
extension Array: XDRCodable {
public func xdrEncode(to encoder: XDREncoder) throws {
try encoder.encode(UInt32(self.count))
for element in self {
try (element as! Encodable).encode(to: encoder)
}
}
public init(fromBinary decoder: XDRDecoder) throws {
guard let binaryElement = Element.self as? Decodable.Type else {
throw XDRDecoder.Error.typeNotConformingToDecodable(Element.self)
}
let count = try decoder.decode(UInt32.self)
self.init()
self.reserveCapacity(Int(count))
for _ in 0 ..< count {
let decoded = try binaryElement.init(from: decoder)
self.append(decoded as! Element)
}
}
}
This gives the following error in swift 4.1:
'XDRDecodable' requires that 'Element' conform to 'Decodable'
So we tried changing the declaration to:
extension Array: XDRCodable where Element : XDRCodable
While this compiles it still fails to encode the array and the following warning is generated:
warning: Swift runtime does not yet support dynamically querying conditional conformance ('Swift.Array': 'stellarsdk.XDREncodable')
I saw that this is a work in progress but does anyone have a workaround for this until type conformance is properly implemented. I'd like it to work as it was in swift 4.0 for now.