How to solve redundant conformance to protocol error when including Swift package?

Viewed 123

In my own code I have this:

extension CLLocationCoordinate2D: Codable {
    
    enum Keys: String, CodingKey {
        case latitude = "latitude"
        case longitude = "longitude"
    }
    
    public init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: Keys.self)
        let latitudeVal: Double = try container.decode(Double.self, forKey: .latitude)
        let longitudeVal: Double = try container.decode(Double.self, forKey: .longitude)
        
        self.init(latitude: latitudeVal, longitude: longitudeVal)
    }
    
    public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: Keys.self)
        try container.encode(latitude, forKey: .latitude)
        try container.encode(longitude, forKey: .longitude)
    }
}

Now I want to include Mapbox in my app and I added it through Swift Package Manager.

After doing this however I get a redundant conformance to protocol error because Mapbox includes this:

extension CLLocationCoordinate2D: Codable {
    public func encode(to encoder: Encoder) throws {
        var container = encoder.unkeyedContainer()
        try container.encode(longitude)
        try container.encode(latitude)
    }
    
    public init(from decoder: Decoder) throws {
        var container = try decoder.unkeyedContainer()
        let longitude = try container.decode(CLLocationDegrees.self)
        let latitude = try container.decode(CLLocationDegrees.self)
        self.init(latitude: latitude, longitude: longitude)
    }
}

Is there a way to restrict this implementation to the imported package, so I can keep using my own extension? Can this be scoped somehow?

1 Answers

Is there a way to restrict this implementation to the imported package, so I can keep using my own extension? Can this be scoped somehow?

No. Protocol conformances must be public if the type is public. A type cannot conform to a protocol only within a certain scope. What's wrong with with the implementation of Codable in Mapbox?

You could fork the repository and remove this code in the fork.

Related