How to check for Identifiable conformance

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 =
    } 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?

Here's one way to do this, by defining separate fetch methods:

func fetch<T: Codable & Identifiable>(_ object: T) throws -> SomeType where T.ID == String {
	return try performFetch(

func fetch<T: Codable & Hashable>(_ object: T) throws -> SomeType {
	return try performFetch(String(object.hashValue))

private func performFetch(_ id: String) throws -> SomeType { ... }
Terms of Service

Privacy Policy

Cookie Policy