I have two distinct types that represent the same data, and have the exact same "shape". The two distinct types are code-gen'd and I am forced to deal with them. But, I want to make them conform to a common protocol so that I can treat both types the same. Here's an example:
Let's say that these are my two code-gen'd types, which I am stuck with:
struct User1 {
var email: String
var name: Name
struct Name {
var givenName: String
var familyName: String
}
}
struct User2 {
var email: String
var name: Name
struct Name {
var givenName: String
var familyName: String
}
}
I want to be able to use these types interchangeably, so I create a couple protocols that they can conform to:
protocol NameRepresenting {
var givenName: String { get }
var familyName: String { get }
}
protocol UserRepresenting {
var email: String { get }
var name: NameRepresenting { get }
}
And then I attempt to make them conform:
extension User1.Name: NameRepresenting {}
// Error: Type 'User1' does not conform to protocol 'UserRepresenting'
extension User1: UserRepresenting {}
extension User2.Name: NameRepresenting {}
// Error: Type 'User2' does not conform to protocol 'UserRepresenting'
extension User2: UserRepresenting {}
I expect the above to work, but compilation fails with the errors commented above. Is there any elegant way to get these two types to conform to a common protocol so I can use them interchangeably?