Protocol can an only be used as a generic constraint because it has Self or associated type requirements
As I mentioned in the comment. What is missing in your code is that the associatedtype
never actually becomes a type. Nowhere in the code in one of your structs
you assign the type to the associatedtype
. If you want a generic filterable
functionality you could do something along those lines:
// Your generic Filters with required properties
protocol Filters: Encodable {
var isWithdrawal: Bool { get }
init(isWithdrawal: Bool)
}
// Your generic query parameters
protocol QueryParameters: Encodable {
associatedtype F: Filters
var page: Int { get }
var filters: F { get }
init(page: Int, filters: Filters)
}
// Filterable protocol will eventually accept any types conforming to Filters and QueryParameters
protocol Filterable {
associatedtype F: Filters
associatedtype P: QueryParameters
func parameters() -> P
}
// This is your generic Transactions struct
// With this you will be able to pass other types that meet the constraints
struct Transactions<ParameterType: QueryParameters>: Filterable {
typealias P = ParameterType
typealias F = ParameterType.F
func parameters() -> ParameterType {
return P(page: 1, filters: F(isWithdrawal: true))
}
}
You're done with the generic stuff. Now you can create different model objects which conform to your protocols
struct TransactionFilters: Filters {
private(set) var isWithdrawal: Bool // Conforming to filters
}
struct TransactionParameters<FilterType: Filters>: QueryParameters {
// Telling what type is the object that conforms to Filters
typealias F = FilterType
var page: Int
var filters: FilterType
init(page: Int, filters: Filters) {
self.page = page
self.filters = filters as! F
}
}
Create your transactions object like this:
let transactions = Transactions<TransactionParameters<TransactionFilters>>()
print(transactions.parameters().page)
print(transactions.parameters().filters.isWithdrawal)
You can create more types of QueryParameters and Filters
struct SomeOtherParameters<FilterType: Filters>: QueryParameters {
// You can do custom stuff in your SomeOtherParameters struct
// e.g. add an offset to page
typealias F = FilterType
var page: Int
var filters: FilterType
init(page: Int, filters: Filters) {
self.page = page + 100
self.filters = filters as! F
}
}
// Your special filter that always returns false
struct SomeOtherFilters: Filters {
init(isWithdrawal: Bool) {}
var isWithdrawal: Bool {
return false
}
}
let transactionsWithDifferentFilters = Transactions<SomeOtherParameters<SomeOtherFilters>>()
// You can combine any types that conform to you declared protocols
let evenMoreTransactions = Transactions<SomeOtherParameters<TransactionFilters>>()
print(evenMoreTransactions.parameters().page)