Swift-Kuery-ORM icon indicating copy to clipboard operation
Swift-Kuery-ORM copied to clipboard

Suggestion: A better way to handle the Model protocol.

Open xeokeri opened this issue 6 years ago • 0 comments

Context and Description

Trying to create a project where the Query filtering components and the Model components, that are outside of the scope of SwiftKuery/SwiftKueryORM/etc..., are in individual frameworks. The framework that mainly interacts with the Models in my implementation, doesn't allow for the models to directly access the Query.framework. Query.framework has access to Models.framework, but Models.framework does not have access to Query.framework. Both have access to SwiftKuery/SwiftQueryORM/etc..

There are additional enhancements to filtering, beyond filtering by ID as well as additional updateOrInsert handling that I've worked on. Though, the models don't have access to this. To solve my problem, I'd need to flip it, so that Models.framework imports Query.framework, and then add an extension to the SwiftKueryORM.Model to be able to do the work that Query.framework is already doing. I would prefer that the models be agnostic to the Query.framework, but it seems that is not the way SwiftKueryORM.Model wants it. Spent 12 hours trying to find a way around it, but it seems that all the Self references in the Model.swift protocol and extension prevent that.

Environment Details

Running macOS 10.14.1, Xcode 10.1, Apple Swift version 4.2.1 (swiftlang-1000.11.42 clang-1000.11.45.1)

Steps to Reproduce

  1. Create a method that returns Model.Type or a method that accepts a parameter as Model
  2. Swift errors will be displayed.

Expected vs. Actual Behaviour

  • [ ] Expected: Ability to pass Model to a method without having to use extensions on the model.
  • [ ] Actual: Error message displays saying the protocol can't be used as a parameter or even as a return type, such as Model.Type
public struct RandomModel : Model
    public fileprivate(set) var date: Date
    public fileprivate(set) var updated: Date
    public fileprivate(set) var uuid: UUID?
    // ...
}

public final class RandomClass {
    // error displayed for the return value.
    func modelType() -> Model.Type {
        return RandomModel.self
    }
    
    // error displayed for the parameter type.
    func handleModel(_ model: Model) {
        // ...
    }
}

Error Message Displayed

Protocol 'Model' can only be used as a generic constraint because it has Self or associated type requirements

xeokeri avatar Dec 02 '18 08:12 xeokeri