Problem with type erased protocols

Hi All.

I'm trying to write a model-driven XML unarchiver. I've created a network of protocols to make this work: Prop, Props, Model, and Builder. From reading on the net, it looks like the way to properly handle protocols with associated types is to create a type erased protocol for your protocol to inherit from. To that end, I also have AnyProp, AnyProps, AnyModel, and AnyBuilder with none of these requirements. Unfortunately, I am now stuck. Here is a code sample:

public protocol HXAnyModelProtocol {
    static var anyProps:HXAnyPropsProtocol {get}
    static func anyBuilder() -> HXAnyBuilderProtocol
    func anyBuilder<T:HXAnyModelProtocol>(forKey key:KeyPath<Self,T>) throws -> HXAnyBuilderProtocol
}

public protocol HXModelProtocol : HXAnyModelProtocol {
    associatedtype PropType:HXPropProtocol
    associatedtype BuilderType:HXBuilderProtocol
    
    static var props:HXProps<PropType> {get}
    static func builder() -> BuilderType
    func builder<T:HXModelProtocol>(forKey key:KeyPath<Self,T>) throws -> T.BuilderType
}

public extension HXModelProtocol {
    static var anyProps:HXAnyPropsProtocol {return self.props}
    static func anyBuilder() -> HXAnyBuilderProtocol {return self.builder()}
    func anyBuilder<T:HXAnyModelProtocol>(forKey key:KeyPath<Self,T>) throws -> HXAnyBuilderProtocol {
        let x:KeyPath<Self,HXModelProtocol> = key as! KeyPath<Self,HXModelProtocol>
        ERROR: HXModelProtocol can only be used as a generic constraint...
        return try self.builder(forKey:x)
        
    }
}

Is there a trick for downcasting T:HXAnyModelProtocol to T:HXModelProtocol? There's always a trick... Thanks in advance for any help.

I’m not sure what exactly you read, but in my experience the common practice is to create a type-erased struct. For example, the standard library has AnyCollection and AnyHashable types, both of which are structs.