So here's the goal: I have a framework that deals in subclasses of a base `Object`. While I want to use this framework, I do not want to expose it to the rest of the app. Instead, I'm writing a layer around the framework that uses `Model` objects. In order to do this, I need to add a translation layer. I want the translation layer to be a protocol that simply translates from an `Object` subtype to a `Model` subtype. Ex:
public protocol Translator {
typealias T:Model
typealias U:Object
var objectType:U.Type { get }
var modelType:T.Type { get }
func translate(from: U) -> T
func translate(from: T) -> U
}I can't store the protocol this way though, so I tried to make it more generic:
public protocol Translator {
var objectType:Object.Type { get }
var modelType:Model.Type { get }
func translateObject(from: Object) -> Model
func translateModel(from: Model) -> Object
}I would then have a wrapper class that stores the Translators and uses them to convert from `Object` to `Model`:
public class Wrapper {
let translators:[Translator]
init(translators:[Translator]) {
self.translators = translators
}
private func translatorFor<T:Model>(type: T.Type) -> Translator? {
for translator in translators {
if translator.modelType === type {
return translator
}
}
return nil
}
private func getObject<U:Object>(type: U.Type) -> U {
// Use framework to return object
}
func getModel<T:Model>(type:T.Type) -> T? {
if let translator = translatorFor(type) {
let object = getObject(translator.objectType)
return translator.translateObject(object) as? T
}
return nil
}
}The idea is simple, the Wrapper takes a Model type, uses it to find a Translator for that type. It uses the Translator to retrieve the correct Object, and the convert that object into a Model.
With a more generic Translator Protocol this almost works, except I can't seem to check the provided type in the provided in `getModel` to the stored type in translator. On line 10 in the Wrapper I get the error `Binary operator '===' cannot be applied to operands of type 'Model.Type' and 'T.Type'`. But I know that `T` conforms to `Model`.
Does anyone have an idea of how I can accomplish this?