I'd like to check if a type conforms to "Identifiable where ID == String" from a template method that has a signature similar to:
func fetch<T: Codable>(_ object: T) throws -> SomeOtherType {
let id: String
if let identified = object as? Identifiable where ID == String {
id = identified.id
} else if let anonymous = object as? Hashable {
id = anonymous.hashValue
} else {
throw someError("Type \(type(of: object)) isn't supported")
}
return try performFetch(id)
}
This, of course, does not compile, because reasons about Self or associated type and some such. I happen to want to use this on types that declare conformance to Identifiable and specify id: String, and the performFetch needs the id to be a String.
Do I need to invent some kind of AnyIdentifiable or StringIdentifiable to accomplish this, or am I going about this wrong?